コード例 #1
0
ファイル: AgDocumentView.xaml.cs プロジェクト: 0xJins/kaxaml
        private void Parse(bool IsExplicit)
        {
            ClearDispatcherTimer();

            if (XamlDocument != null && !CodeCompletionPopup.IsOpenSomewhere)
            {
                if (XamlDocument.SourceText != null)
                {
                    bool parseSuccess = false;
                    parseSuccess = (bool)this.ContentArea.InvokeScript("ParseXaml", new object[] { XamlDocument.SourceText });

                    if (parseSuccess)
                    {
                        IsValidXaml       = true;
                        ErrorText         = null;
                        ErrorLineNumber   = 0;
                        ErrorLinePosition = 0;

                        if (Kaxaml.Properties.Settings.Default.EnableAutoBackup)
                        {
                            XamlDocument.SaveBackup();
                        }

                        DelayedSnapshot();
                    }
                    else
                    {
                        ErrorText         = (string)this.ContentArea.InvokeScript("GetLastErrorMessage", null);
                        ErrorLineNumber   = int.Parse((string)this.ContentArea.InvokeScript("GetLastErrorLineNumber", null));
                        ErrorLinePosition = int.Parse((string)this.ContentArea.InvokeScript("GetLastErrorLinePos", null));

                        ErrorText = ErrorText.Replace("\r", "");
                        ErrorText = ErrorText.Replace("\n", "");
                        ErrorText = ErrorText.Replace("\t", "");

                        // get rid of everything after "Line" if it is in the last 30 characters
                        int pos = ErrorText.LastIndexOf("[Line");
                        if (pos > 0 && pos > (ErrorText.Length - 50))
                        {
                            ErrorText = ErrorText.Substring(0, pos);
                        }

                        IsValidXaml = false;
                    }
                }
            }
        }
コード例 #2
0
        private void Parse(bool IsExplicit)
        {
            ClearDispatcherTimer();

            if (XamlDocument != null && !CodeCompletionPopup.IsOpenSomewhere)
            {
                if (XamlDocument.SourceText != null)
                {
                    // handle the in place preparsing (this actually updates the source in the editor)
                    int index = TextEditor.CaretIndex;
                    XamlDocument.SourceText = PreParse(XamlDocument.SourceText);
                    TextEditor.CaretIndex   = index;

                    string str = XamlDocument.SourceText;

                    // handle the in memory preparsing (this happens behind the scenes all in memory)

                    try
                    {
                        object content = null;
                        using (var ms = new MemoryStream(str.Length))
                        {
                            using (var sw = new StreamWriter(ms))
                            {
                                sw.Write(str);
                                sw.Flush();

                                ms.Seek(0, SeekOrigin.Begin);

                                ParserContext pc = new ParserContext();
                                pc.BaseUri = new Uri(XamlDocument.Folder != null
                                    ? XamlDocument.Folder + "/"
                                    : System.Environment.CurrentDirectory + "/");
                                //pc.BaseUri = new Uri(XamlDocument.Folder + "/");
                                //pc.BaseUri = new Uri(System.Environment.CurrentDirectory + "/");

                                ContentArea.JournalOwnership = System.Windows.Navigation.JournalOwnership.UsesParentJournal;
                                content = XamlReader.Load(ms, pc);
                            }
                        }

                        if (content != null && content is Window)
                        {
                            var window = (Window)content;
                            window.Owner = Application.Current.MainWindow;

                            if (!IsExplicit)
                            {
                                Border bd = new Border()
                                {
                                    Background = Color.FromRgb(0x40, 0x40, 0x40).ToCachedBrush()
                                };

                                TextBlock tb = new TextBlock()
                                {
                                    FontFamily          = new FontFamily("Segoe, Segoe UI, Verdana"),
                                    TextAlignment       = TextAlignment.Center,
                                    TextWrapping        = TextWrapping.Wrap,
                                    FontSize            = 14,
                                    Foreground          = Brushes.White,
                                    MaxWidth            = 320,
                                    Margin              = new Thickness(50),
                                    VerticalAlignment   = VerticalAlignment.Center,
                                    HorizontalAlignment = HorizontalAlignment.Center,
                                    Text = "The root element of this content is of type Window.  Press F5 to show the content in a new window."
                                };

                                bd.Child            = tb;
                                ContentArea.Content = bd;
                            }
                            else
                            {
                                window.Show();
                            }
                        }
                        else
                        {
                            ContentArea.Content = content;
                        }

                        IsValidXaml              = true;
                        ErrorText                = null;
                        ErrorLineNumber          = 0;
                        ErrorLinePosition        = 0;
                        UnhandledExceptionRaised = false;

                        if (Kaxaml.Properties.Settings.Default.EnableAutoBackup)
                        {
                            XamlDocument.SaveBackup();
                            //if (!Editor.Text.Equals(DefaultXaml))
                            //{
                            //    File.WriteAllText(XamlDocument.BackupPath, Editor.Text);
                            //}
                        }
                    }

                    catch (Exception ex)
                    {
                        if (ex.IsCriticalException())
                        {
                            throw;
                        }
                        else
                        {
                            ReportError(ex);
                        }
                    }
                }
            }
        }