diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Images/Icons.32x32.Error.png b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Images/Icons.32x32.Error.png new file mode 100644 index 0000000000..28ba9c4e13 Binary files /dev/null and b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Images/Icons.32x32.Error.png differ diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfDocumentError.xaml b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfDocumentError.xaml new file mode 100644 index 0000000000..50a854975a --- /dev/null +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfDocumentError.xaml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + View XAML + + + + \ No newline at end of file diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfDocumentError.xaml.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfDocumentError.xaml.cs new file mode 100644 index 0000000000..e27d15f7cf --- /dev/null +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfDocumentError.xaml.cs @@ -0,0 +1,30 @@ +// +// +// +// +// $Revision: $ +// +using System; +using System.Windows; +using System.Windows.Controls; + +using ICSharpCode.SharpDevelop.Gui; + +namespace ICSharpCode.WpfDesign.AddIn +{ + /// + /// A friendly error window displayed when the WPF document does not parse correctly. + /// + public partial class WpfDocumentError : UserControl + { + public WpfDocumentError() + { + InitializeComponent(); + } + + void ViewXaml(object sender,RoutedEventArgs e) + { + WorkbenchSingleton.Workbench.ActiveWorkbenchWindow.SwitchView(0); + } + } +} \ No newline at end of file diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfViewContent.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfViewContent.cs index a597dfa744..c17d8dcf85 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfViewContent.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/Src/WpfViewContent.cs @@ -6,14 +6,16 @@ // using System; -using System.Windows; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Threading; +using System.Windows; using System.Windows.Input; using System.Windows.Markup; using System.Xml; +using ICSharpCode.Core.Presentation; using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop.Editor; using ICSharpCode.SharpDevelop.Gui; @@ -23,8 +25,6 @@ using ICSharpCode.WpfDesign.Designer.PropertyGrid; using ICSharpCode.WpfDesign.Designer.Services; using ICSharpCode.WpfDesign.Designer.Xaml; -using ICSharpCode.Core.Presentation; - namespace ICSharpCode.WpfDesign.AddIn { /// @@ -42,7 +42,7 @@ namespace ICSharpCode.WpfDesign.AddIn static WpfViewContent() { - DragDropExceptionHandler.UnhandledException += delegate(object sender, ThreadExceptionEventArgs e) { + DragDropExceptionHandler.UnhandledException += delegate(object sender, ThreadExceptionEventArgs e) { ICSharpCode.Core.MessageService.ShowException(e.Exception); }; } @@ -62,12 +62,17 @@ namespace ICSharpCode.WpfDesign.AddIn { Debug.Assert(file == this.PrimaryFile); + _stream = new MemoryStream(); + stream.CopyTo(_stream); + stream.Position = 0; + if (designer == null) { // initialize designer on first load designer = new DesignSurface(); this.UserContent = designer; InitPropertyEditor(); } + this.UserContent=designer; if (outline != null) { outline.Root = null; } @@ -83,52 +88,72 @@ namespace ICSharpCode.WpfDesign.AddIn context.Services.AddService(typeof(ChooseClassServiceBase), new IdeChooseClassService()); }); settings.TypeFinder = MyTypeFinder.Create(this.PrimaryFile); - - designer.LoadDesigner(r, settings); - - designer.ContextMenuOpening += (sender, e) => MenuService.ShowContextMenu(e.OriginalSource as UIElement, designer, "/Addins/WpfDesign/Designer/ContextMenu"); - - UpdateTasks(); - - if (outline != null && designer.DesignContext != null && designer.DesignContext.RootItem != null) { - outline.Root = OutlineNode.Create(designer.DesignContext.RootItem); + try{ + settings.ReportErrors=UpdateTasks; + designer.LoadDesigner(r, settings); + + designer.ContextMenuOpening += (sender, e) => MenuService.ShowContextMenu(e.OriginalSource as UIElement, designer, "/Addins/WpfDesign/Designer/ContextMenu"); + + if (outline != null && designer.DesignContext != null && designer.DesignContext.RootItem != null) { + outline.Root = OutlineNode.Create(designer.DesignContext.RootItem); + } + + var outlineContent = GetService(typeof(IOutlineContentHost)); + if(outlineContent==null) + this.Services.AddService(typeof(IOutlineContentHost),Outline); + + propertyGridView.PropertyGrid.SelectedItems = null; + designer.DesignContext.Services.Selection.SelectionChanged += OnSelectionChanged; + designer.DesignContext.Services.GetService().UndoStackChanged += OnUndoStackChanged; + } + catch + { + this.UserContent=new WpfDocumentError(); } - - var outlineContent = GetService(typeof(IOutlineContentHost)); - if(outlineContent==null) - this.Services.AddService(typeof(IOutlineContentHost),Outline); - - propertyGridView.PropertyGrid.SelectedItems = null; - designer.DesignContext.Services.Selection.SelectionChanged += OnSelectionChanged; - designer.DesignContext.Services.GetService().UndoStackChanged += OnUndoStackChanged; } } + + private MemoryStream _stream; + protected override void SaveInternal(OpenedFile file, System.IO.Stream stream) { - XmlWriterSettings settings = new XmlWriterSettings(); - settings.Indent = true; - settings.IndentChars = EditorControlService.GlobalOptions.IndentationString; - settings.NewLineOnAttributes = true; - using (XmlWriter xmlWriter = XmlTextWriter.Create(stream, settings)) { - designer.SaveDesigner(xmlWriter); + if(designer.DesignContext!=null){ + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Indent = true; + settings.IndentChars = EditorControlService.GlobalOptions.IndentationString; + settings.NewLineOnAttributes = true; + using (XmlWriter xmlWriter = XmlTextWriter.Create(stream, settings)) { + designer.SaveDesigner(xmlWriter); + } + } + else{ + _stream.Position = 0; + using (var reader = new StreamReader(_stream)) + using (var writer = new StreamWriter(stream)) { + writer.Write(reader.ReadToEnd()); + } } } - void UpdateTasks() + void UpdateTasks(XamlErrorService xamlErrorService) { + Debug.Assert(xamlErrorService!=null); foreach (var task in tasks) { TaskService.Remove(task); } tasks.Clear(); - var xamlErrorService = designer.DesignContext.Services.GetService(); foreach (var error in xamlErrorService.Errors) { var task = new Task(PrimaryFile.FileName, error.Message, error.Column - 1, error.Line - 1, TaskType.Error); tasks.Add(task); TaskService.Add(task); } + + if (xamlErrorService.Errors.Count != 0) { + WorkbenchSingleton.Workbench.GetPad(typeof (ErrorListPad)).BringPadToFront(); + } } void OnUndoStackChanged(object sender, EventArgs e) diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/WpfDesign.AddIn.csproj b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/WpfDesign.AddIn.csproj index 333781b053..89afcec4a7 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/WpfDesign.AddIn.csproj +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.AddIn/WpfDesign.AddIn.csproj @@ -52,6 +52,7 @@ + Always @@ -85,11 +86,16 @@ + + WpfDocumentError.xaml + Code + + {2748AD25-9C63-4E12-877B-4DCE96FBED54} ICSharpCode.SharpDevelop @@ -145,6 +151,7 @@ + diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlDesignContext.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlDesignContext.cs index 9e81c2a4ce..54dc9f2342 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlDesignContext.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlDesignContext.cs @@ -25,7 +25,7 @@ namespace ICSharpCode.WpfDesign.Designer.Xaml public sealed class XamlDesignContext : DesignContext { readonly XamlDocument _doc; - readonly XamlDesignItem _rootItem; + readonly XamlDesignItem _rootItem; internal readonly XamlComponentService _componentService; readonly XamlEditOperations _xamlEditOperations; @@ -90,11 +90,14 @@ namespace ICSharpCode.WpfDesign.Designer.Xaml parserSettings.CreateInstanceCallback = this.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory; parserSettings.ServiceProvider = this.Services; _doc = XamlParser.Parse(xamlReader, parserSettings); - _rootItem = _componentService.RegisterXamlComponentRecursive(_doc.RootElement); + if(_doc==null) + loadSettings.ReportErrors(xamlErrorService); - _xamlEditOperations=new XamlEditOperations(this,parserSettings); + _rootItem = _componentService.RegisterXamlComponentRecursive(_doc.RootElement); + + _xamlEditOperations=new XamlEditOperations(this,parserSettings); } - + /// /// Saves the XAML DOM into the XML writer. diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlLoadSettings.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlLoadSettings.cs index 281526a8f4..ed37263d1a 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlLoadSettings.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlLoadSettings.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Reflection; +using ICSharpCode.WpfDesign.Designer.Services; using ICSharpCode.WpfDesign.XamlDom; namespace ICSharpCode.WpfDesign.Designer.Xaml @@ -19,6 +20,7 @@ namespace ICSharpCode.WpfDesign.Designer.Xaml { public readonly ICollection DesignerAssemblies = new List(); public readonly List> CustomServiceRegisterFunctions = new List>(); + public Action ReportErrors; XamlTypeFinder typeFinder = XamlTypeFinder.CreateWpfTypeFinder(); public XamlTypeFinder TypeFinder {