Browse Source

A few small fixes for the XamlDesigner Sample App:

- fix the AvalonDock default Settings so no exception is raised
- Allow the Designed WPF View to be Tested or Rendered to a Bitmap
- Better Assembly Resolving
pull/449/head
jkuehner 12 years ago
parent
commit
eae49c832c
  1. 67
      samples/XamlDesigner/App.xaml.cs
  2. 35
      samples/XamlDesigner/Configuration/Settings.Designer.cs
  3. 35
      samples/XamlDesigner/Configuration/Settings.settings
  4. 10
      samples/XamlDesigner/MainWindow.xaml
  5. 79
      samples/XamlDesigner/MainWindow_Commands.cs

67
samples/XamlDesigner/App.xaml.cs

@ -2,11 +2,16 @@ using System; @@ -2,11 +2,16 @@ using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows;
using ICSharpCode.XamlDesigner.Configuration;
using System.Windows.Threading;
using System.Diagnostics;
using ICSharpCode.WpfDesign.Designer;
using ICSharpCode.XamlDesigner.Configuration;
namespace ICSharpCode.XamlDesigner
{
@ -16,11 +21,69 @@ namespace ICSharpCode.XamlDesigner @@ -16,11 +21,69 @@ namespace ICSharpCode.XamlDesigner
protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(AppDomain_CurrentDomain_AssemblyResolve);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomain_CurrentDomain_UnhandledException);
DragDropExceptionHandler.UnhandledException += new ThreadExceptionEventHandler(DragDropExceptionHandler_UnhandledException);
DispatcherUnhandledException += App_DispatcherUnhandledException;
Args = e.Args;
base.OnStartup(e);
}
private static bool internalLoad = false;
private static string lastRequesting = null;
Assembly AppDomain_CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (internalLoad)
return null;
if (args.Name.Split(new [] { ',' })[0].Trim().EndsWith(".resources"))
return null;
internalLoad = true;
Assembly ass = null;
try {
ass = Assembly.Load(args.Name);
}
catch (Exception) { }
if (ass == null && args.RequestingAssembly != null) {
lastRequesting = args.RequestingAssembly.Location;
var dir = Path.GetDirectoryName(args.RequestingAssembly.Location);
var file = args.Name.Split(new [] { ',' })[0].Trim() + ".dll";
try {
ass = Assembly.LoadFrom(Path.Combine(dir, file));
}
catch (Exception) { }
}
else if (lastRequesting != null) {
var dir = Path.GetDirectoryName(lastRequesting);
var file = args.Name.Split(new [] { ',' })[0].Trim() + ".dll";
try {
ass = Assembly.LoadFrom(Path.Combine(dir, file));
}
catch (Exception) { }
}
internalLoad = false;
return ass;
}
void DragDropExceptionHandler_UnhandledException(object sender, ThreadExceptionEventArgs e)
{
Shell.ReportException(e.Exception);
}
void AppDomain_CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Shell.ReportException(e.ExceptionObject as Exception);
}
void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
Shell.ReportException(e.Exception);

35
samples/XamlDesigner/Configuration/Settings.Designer.cs generated

@ -37,25 +37,34 @@ namespace ICSharpCode.XamlDesigner.Configuration { @@ -37,25 +37,34 @@ namespace ICSharpCode.XamlDesigner.Configuration {
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute(@"<DockingManager>
<ResizingPanel Orientation=""Horizontal"">
<ResizingPanel ResizeWidth=""200"" Orientation=""Vertical"">
<DockablePane ResizeHeight=""441.36166666666668"" Anchor=""Left"">
<DockableContent Name=""content1"" AutoHide=""false"" />
[global::System.Configuration.DefaultSettingValueAttribute(@"<DockingManager version=""1.3.0"">
<ResizingPanel ResizeWidth=""*"" ResizeHeight=""*"" EffectiveSize=""0,0"" Orientation=""Vertical"">
<ResizingPanel ResizeWidth=""*"" ResizeHeight=""*"" EffectiveSize=""1920,1153.04"" Orientation=""Horizontal"">
<ResizingPanel ResizeWidth=""250"" ResizeHeight=""*"" EffectiveSize=""250,1153.04"" Orientation=""Vertical"">
<DockablePane ResizeWidth=""0.742440632565806*"" ResizeHeight=""0.742440632565806*"" EffectiveSize=""250,851.609103178282"" ID=""bb7a5fd3-be24-4636-8fd6-a50a09e65e69"" Anchor=""Left"" IsAutoHidden=""false"">
<DockableContent Name=""content1"" FloatingWindowSize=""250,400"" ChildIndex=""0"" Width=""315"" Height=""1153.04"" Anchor=""Right"" State=""Docked"" />
</DockablePane>
<DockablePane ResizeWidth=""200"" Anchor=""Left"">
<DockableContent Name=""content2"" AutoHide=""false"" />
<DockablePane ResizeWidth=""0.257559367434194*"" ResizeHeight=""0.257559367434194*"" EffectiveSize=""250,295.430896821718"" ID=""17cd1941-1004-4ed9-8cda-03c254681289"" Anchor=""Left"" IsAutoHidden=""false"">
<DockableContent Name=""content2"" FloatingWindowSize=""250,400"" ChildIndex=""0"" Width=""328"" Height=""1153.04"" Anchor=""Right"" State=""Docked"" />
</DockablePane>
</ResizingPanel>
<ResizingPanel Orientation=""Vertical"">
<DocumentPanePlaceHolder />
<DockablePane ResizeHeight=""138"" Anchor=""Bottom"">
<DockableContent Name=""content3"" AutoHide=""false"" />
<ResizingPanel ResizeWidth=""*"" ResizeHeight=""*"" EffectiveSize=""1408,1153.04"" Orientation=""Vertical"">
<DocumentPaneResizingPanel ResizeWidth=""*"" ResizeHeight=""*"" EffectiveSize=""1408,965.04"" Orientation=""Vertical"">
<DocumentPane IsMain=""true"" ResizeWidth=""*"" ResizeHeight=""*"" EffectiveSize=""1408,965.04"" />
</DocumentPaneResizingPanel>
<DockablePane ResizeWidth=""*"" ResizeHeight=""182"" EffectiveSize=""1408,182"" ID=""ebd34b7e-6a89-42c6-b172-0e666b0d8a0a"" Anchor=""Bottom"" IsAutoHidden=""false"">
<DockableContent Name=""content3"" FloatingWindowSize=""250,400"" ChildIndex=""0"" Width=""1920"" Height=""400"" Anchor=""Bottom"" State=""Docked"" />
</DockablePane>
</ResizingPanel>
<DockablePane ResizeWidth=""271"" Anchor=""Right"">
<DockableContent Name=""content4"" AutoHide=""false"" />
<ResizingPanel ResizeWidth=""250"" ResizeHeight=""*"" EffectiveSize=""250,1153.04"" Orientation=""Vertical"">
<DockablePane ResizeWidth=""0.145251345356991*"" ResizeHeight=""0.145251345356991*"" EffectiveSize=""250,166.609103178283"" ID=""aa545474-48b3-49a6-b76c-b0c625e79e4c"" Anchor=""Right"" IsAutoHidden=""false"">
<DockableContent Name=""content5"" FloatingWindowSize=""250,400"" ChildIndex=""0"" Width=""550.666666666667"" Height=""1153.04"" Anchor=""Right"" State=""Docked"" />
</DockablePane>
<DockablePane ResizeWidth=""0.854748654643009*"" ResizeHeight=""0.854748654643009*"" EffectiveSize=""250,980.430896821717"" ID=""64e5518b-2c83-4e22-908d-a510a6995c27"" Anchor=""Right"" IsAutoHidden=""false"">
<DockableContent Name=""content4"" FloatingWindowSize=""250,400"" ChildIndex=""0"" Width=""399.967430639324"" Height=""1153.04"" Anchor=""Right"" State=""Docked"" />
</DockablePane>
</ResizingPanel>
</ResizingPanel>
</ResizingPanel>
<Hidden />
<Windows />

35
samples/XamlDesigner/Configuration/Settings.settings

@ -6,25 +6,34 @@ @@ -6,25 +6,34 @@
<Value Profile="(Default)">0,0,0,0</Value>
</Setting>
<Setting Name="AvalonDockLayout" Type="System.String" Scope="User">
<Value Profile="(Default)">&lt;DockingManager&gt;
&lt;ResizingPanel Orientation="Horizontal"&gt;
&lt;ResizingPanel ResizeWidth="200" Orientation="Vertical"&gt;
&lt;DockablePane ResizeHeight="441.36166666666668" Anchor="Left"&gt;
&lt;DockableContent Name="content1" AutoHide="false" /&gt;
<Value Profile="(Default)">&lt;DockingManager version="1.3.0"&gt;
&lt;ResizingPanel ResizeWidth="*" ResizeHeight="*" EffectiveSize="0,0" Orientation="Vertical"&gt;
&lt;ResizingPanel ResizeWidth="*" ResizeHeight="*" EffectiveSize="1920,1153.04" Orientation="Horizontal"&gt;
&lt;ResizingPanel ResizeWidth="250" ResizeHeight="*" EffectiveSize="250,1153.04" Orientation="Vertical"&gt;
&lt;DockablePane ResizeWidth="0.742440632565806*" ResizeHeight="0.742440632565806*" EffectiveSize="250,851.609103178282" ID="bb7a5fd3-be24-4636-8fd6-a50a09e65e69" Anchor="Left" IsAutoHidden="false"&gt;
&lt;DockableContent Name="content1" FloatingWindowSize="250,400" ChildIndex="0" Width="315" Height="1153.04" Anchor="Right" State="Docked" /&gt;
&lt;/DockablePane&gt;
&lt;DockablePane ResizeWidth="200" Anchor="Left"&gt;
&lt;DockableContent Name="content2" AutoHide="false" /&gt;
&lt;DockablePane ResizeWidth="0.257559367434194*" ResizeHeight="0.257559367434194*" EffectiveSize="250,295.430896821718" ID="17cd1941-1004-4ed9-8cda-03c254681289" Anchor="Left" IsAutoHidden="false"&gt;
&lt;DockableContent Name="content2" FloatingWindowSize="250,400" ChildIndex="0" Width="328" Height="1153.04" Anchor="Right" State="Docked" /&gt;
&lt;/DockablePane&gt;
&lt;/ResizingPanel&gt;
&lt;ResizingPanel Orientation="Vertical"&gt;
&lt;DocumentPanePlaceHolder /&gt;
&lt;DockablePane ResizeHeight="138" Anchor="Bottom"&gt;
&lt;DockableContent Name="content3" AutoHide="false" /&gt;
&lt;ResizingPanel ResizeWidth="*" ResizeHeight="*" EffectiveSize="1408,1153.04" Orientation="Vertical"&gt;
&lt;DocumentPaneResizingPanel ResizeWidth="*" ResizeHeight="*" EffectiveSize="1408,965.04" Orientation="Vertical"&gt;
&lt;DocumentPane IsMain="true" ResizeWidth="*" ResizeHeight="*" EffectiveSize="1408,965.04" /&gt;
&lt;/DocumentPaneResizingPanel&gt;
&lt;DockablePane ResizeWidth="*" ResizeHeight="182" EffectiveSize="1408,182" ID="ebd34b7e-6a89-42c6-b172-0e666b0d8a0a" Anchor="Bottom" IsAutoHidden="false"&gt;
&lt;DockableContent Name="content3" FloatingWindowSize="250,400" ChildIndex="0" Width="1920" Height="400" Anchor="Bottom" State="Docked" /&gt;
&lt;/DockablePane&gt;
&lt;/ResizingPanel&gt;
&lt;DockablePane ResizeWidth="271" Anchor="Right"&gt;
&lt;DockableContent Name="content4" AutoHide="false" /&gt;
&lt;ResizingPanel ResizeWidth="250" ResizeHeight="*" EffectiveSize="250,1153.04" Orientation="Vertical"&gt;
&lt;DockablePane ResizeWidth="0.145251345356991*" ResizeHeight="0.145251345356991*" EffectiveSize="250,166.609103178283" ID="aa545474-48b3-49a6-b76c-b0c625e79e4c" Anchor="Right" IsAutoHidden="false"&gt;
&lt;DockableContent Name="content5" FloatingWindowSize="250,400" ChildIndex="0" Width="550.666666666667" Height="1153.04" Anchor="Right" State="Docked" /&gt;
&lt;/DockablePane&gt;
&lt;DockablePane ResizeWidth="0.854748654643009*" ResizeHeight="0.854748654643009*" EffectiveSize="250,980.430896821717" ID="64e5518b-2c83-4e22-908d-a510a6995c27" Anchor="Right" IsAutoHidden="false"&gt;
&lt;DockableContent Name="content4" FloatingWindowSize="250,400" ChildIndex="0" Width="399.967430639324" Height="1153.04" Anchor="Right" State="Docked" /&gt;
&lt;/DockablePane&gt;
&lt;/ResizingPanel&gt;
&lt;/ResizingPanel&gt;
&lt;/ResizingPanel&gt;
&lt;Hidden /&gt;
&lt;Windows /&gt;

10
samples/XamlDesigner/MainWindow.xaml

@ -42,6 +42,12 @@ @@ -42,6 +42,12 @@
<CommandBinding Command="Default:MainWindow.ExitCommand"
Executed="ExitCommand_Executed" />
<CommandBinding Command="Default:MainWindow.RunCommand"
Executed="RunCommand_Executed" />
<CommandBinding Command="Default:MainWindow.RenderToBitmapCommand"
Executed="RenderToBitmapCommand_Executed" />
</Window.CommandBindings>
<DockPanel>
@ -78,6 +84,10 @@ @@ -78,6 +84,10 @@
<MenuItem Command="Default:MainWindow.RefreshCommand" />
<MenuItem Command="Find" />
</MenuItem>
<MenuItem Header="Test">
<MenuItem Command="Default:MainWindow.RunCommand" />
<MenuItem Command="Default:MainWindow.RenderToBitmapCommand" />
</MenuItem>
</Menu>
<AvalonDock:DockingManager x:Name="uxDockingManager">

79
samples/XamlDesigner/MainWindow_Commands.cs

@ -1,9 +1,18 @@ @@ -1,9 +1,18 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml;
using Microsoft.Win32;
namespace ICSharpCode.XamlDesigner
{
@ -13,6 +22,8 @@ namespace ICSharpCode.XamlDesigner @@ -13,6 +22,8 @@ namespace ICSharpCode.XamlDesigner
public static SimpleCommand SaveAllCommand = new SimpleCommand("Save All", ModifierKeys.Control | ModifierKeys.Shift, Key.S);
public static SimpleCommand ExitCommand = new SimpleCommand("Exit");
public static SimpleCommand RefreshCommand = new SimpleCommand("Refresh", Key.F5);
public static SimpleCommand RunCommand = new SimpleCommand("Run", ModifierKeys.Shift, Key.F5);
public static SimpleCommand RenderToBitmapCommand = new SimpleCommand("Render to Bitmap");
static void RenameCommands()
{
@ -60,6 +71,72 @@ namespace ICSharpCode.XamlDesigner @@ -60,6 +71,72 @@ namespace ICSharpCode.XamlDesigner
Shell.Instance.SaveAll();
}
void RunCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
StringBuilder sb = new StringBuilder();
var xmlWriter = XmlWriter.Create(new StringWriter(sb));
Shell.Instance.CurrentDocument.DesignSurface.SaveDesigner(xmlWriter);
var txt = sb.ToString();
var xmlReader = XmlReader.Create(new StringReader(txt));
var ctl = XamlReader.Load(xmlReader);
Window wnd = ctl as Window;
if (wnd == null) {
wnd = new Window();
wnd.Content = ctl;
}
wnd.Show();
}
void RenderToBitmapCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
int desiredWidth = 300;
int desiredHeight = 300;
StringBuilder sb = new StringBuilder();
var xmlWriter = XmlWriter.Create(new StringWriter(sb));
Shell.Instance.CurrentDocument.DesignSurface.SaveDesigner(xmlWriter);
var txt = sb.ToString();
var xmlReader = XmlReader.Create(new StringReader(txt));
var ctl = XamlReader.Load(xmlReader) as Control;
if (ctl is Window) {
var wnd = ctl as Window;
wnd.Width = desiredWidth;
wnd.Height = desiredHeight;
wnd.Top = -10000;
wnd.Left = -10000;
wnd.Show();
} else {
ctl.Measure(new Size(desiredWidth, desiredHeight));
ctl.Arrange(new Rect(new Size(desiredWidth, desiredHeight)));
}
RenderTargetBitmap bmp = new RenderTargetBitmap(300, 300, 96, 96, PixelFormats.Default);
bmp.Render(ctl);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
var dlg = new SaveFileDialog();
dlg.Filter = "*.png|*.png";
if (dlg.ShowDialog() == true) {
using (Stream stm = File.OpenWrite(dlg.FileName)) {
encoder.Save(stm);
stm.Flush();
}
}
if (ctl is Window) {
var wnd = ctl as Window;
wnd.Close();
}
}
void ExitCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
Shell.Instance.Exit();

Loading…
Cancel
Save