private static void UpdateXaml(string xaml) { Window window; Control original; using (PlatformManager.DesignerMode()) { var loader = new PerspexXamlLoader(); var stream = new MemoryStream(Encoding.UTF8.GetBytes(xaml)); original = (Control)loader.Load(stream); window = original as Window; if (window == null) { window = new Window() { Content = original }; } if (!window.IsSet(Window.SizeToContentProperty)) { window.SizeToContent = SizeToContent.WidthAndHeight; } } s_currentWindow?.Close(); s_currentWindow = window; window.Show(); Design.ApplyDesignerProperties(window, original); Api.OnWindowCreated?.Invoke(window.PlatformImpl.Handle.Handle); Api.OnResize?.Invoke(); }
private static void UpdateXaml(string xaml) { Window window; Control original; using (PlatformManager.DesignerMode()) { original = (Control)((XamlXmlLoader) new PerspexXamlLoader()).Load(new MemoryStream(Encoding.UTF8.GetBytes(xaml))); window = original as Window; if (window == null) { window = new Window() { Content = original }; } } s_currentWindow?.Close(); s_currentWindow = window; window.Show(); Design.ApplyDesignerProperties(window, original); Api.OnWindowCreated?.Invoke(window.PlatformImpl.Handle.Handle); Api.OnResize?.Invoke(); }
public static Window LoadDesignerWindow(string xaml, string assemblyPath) { Window window; Control control; using (PlatformManager.DesignerMode()) { var loader = new AvaloniaXamlLoader() { IsDesignMode = true }; var stream = new MemoryStream(Encoding.UTF8.GetBytes(xaml)); Uri baseUri = null; if (assemblyPath != null) { //Fabricate fake Uri baseUri = new Uri("resm:Fake.xaml?assembly=" + Path.GetFileNameWithoutExtension(assemblyPath)); } var localAsm = assemblyPath != null?Assembly.LoadFile(Path.GetFullPath(assemblyPath)) : null; var loaded = loader.Load(stream, localAsm, null, baseUri); var styles = loaded as Styles; if (styles != null) { var substitute = styles.OfType <Style>().Select(Design.GetPreviewWith).FirstOrDefault(s => s != null); if (substitute != null) { substitute.Styles.AddRange(styles); control = substitute; } else { control = new StackPanel { Children = { new TextBlock { Text = "Styles can't be previewed without Design.PreviewWith. Add" }, new TextBlock { Text = "<Design.PreviewWith>" }, new TextBlock { Text = " <Border Padding=20><!-- YOUR CONTROL FOR PREVIEW HERE--></Border>" }, new TextBlock { Text = "<Design.PreviewWith>" }, new TextBlock { Text = "before setters in your first Style" } } } }; } else if (loaded is Application) { control = new TextBlock { Text = "Application can't be previewed in design view" } } ; else { control = (Control)loaded; } window = control as Window; if (window == null) { window = new Window() { Content = (Control)control }; } if (!window.IsSet(Window.SizeToContentProperty)) { window.SizeToContent = SizeToContent.WidthAndHeight; } } window.Show(); Design.ApplyDesignModeProperties(window, control); return(window); }
private static void UpdateXaml2(Dictionary <string, object> dic) { var xamlInfo = new DesignerApiXamlFileInfo(dic); Window window; Control control; using (PlatformManager.DesignerMode()) { var loader = new AvaloniaXamlLoader(); var stream = new MemoryStream(Encoding.UTF8.GetBytes(xamlInfo.Xaml)); Uri baseUri = null; if (xamlInfo.AssemblyPath != null) { //Fabricate fake Uri baseUri = new Uri("resm:Fake.xaml?assembly=" + Path.GetFileNameWithoutExtension(xamlInfo.AssemblyPath)); } var loaded = loader.Load(stream, null, baseUri); var styles = loaded as Styles; if (styles != null) { var substitute = Design.GetPreviewWith(styles) ?? styles.Select(Design.GetPreviewWith).FirstOrDefault(s => s != null); if (substitute != null) { substitute.Styles.AddRange(styles); control = substitute; } else { control = new StackPanel { Children = { new TextBlock { Text = "Styles can't be previewed without Design.PreviewWith. Add" }, new TextBlock { Text = "<Design.PreviewWith>" }, new TextBlock { Text = " <Border Padding=20><!-- YOUR CONTROL FOR PREVIEW HERE--></Border>" }, new TextBlock { Text = "<Design.PreviewWith>" }, new TextBlock { Text = "before setters in your first Style" } } } }; } if (loaded is Application) { control = new TextBlock { Text = "Application can't be previewed in design view" } } ; else { control = (Control)loaded; } window = control as Window; if (window == null) { window = new Window() { Content = (Control)control }; } if (!window.IsSet(Window.SizeToContentProperty)) { window.SizeToContent = SizeToContent.WidthAndHeight; } } s_currentWindow?.Close(); s_currentWindow = window; window.Show(); Design.ApplyDesignerProperties(window, control); Api.OnWindowCreated?.Invoke(window.PlatformImpl.Handle.Handle); Api.OnResize?.Invoke(); } }
public static Window LoadDesignerWindow(string xaml, string assemblyPath, string xamlFileProjectPath) { Window window; Control control; using (PlatformManager.DesignerMode()) { var loader = AvaloniaLocator.Current.GetService <AvaloniaXamlLoader.IRuntimeXamlLoader>(); var stream = new MemoryStream(Encoding.UTF8.GetBytes(xaml)); if (loader == null) { throw new XamlLoadException("Runtime XAML loader is not registered"); } Uri baseUri = null; if (assemblyPath != null) { if (xamlFileProjectPath == null) { xamlFileProjectPath = "/Designer/Fake.xaml"; } //Fabricate fake Uri baseUri = new Uri($"avares://{Path.GetFileNameWithoutExtension(assemblyPath)}{xamlFileProjectPath}"); } var localAsm = assemblyPath != null?Assembly.LoadFile(Path.GetFullPath(assemblyPath)) : null; var loaded = loader.Load(stream, localAsm, null, baseUri, true); var style = loaded as IStyle; if (style != null) { var substitute = Design.GetPreviewWith((AvaloniaObject)style); if (substitute != null) { substitute.Styles.Add(style); control = substitute; } else { control = new StackPanel { Children = { new TextBlock { Text = "Styles can't be previewed without Design.PreviewWith. Add" }, new TextBlock { Text = "<Design.PreviewWith>" }, new TextBlock { Text = " <Border Padding=20><!-- YOUR CONTROL FOR PREVIEW HERE --></Border>" }, new TextBlock { Text = "</Design.PreviewWith>" }, new TextBlock { Text = "before setters in your first Style" } } } }; } else if (loaded is Application) { control = new TextBlock { Text = "Application can't be previewed in design view" } } ; else { control = (Control)loaded; } window = control as Window; if (window == null) { window = new Window() { Content = (Control)control }; } Design.ApplyDesignModeProperties(window, control); if (!window.IsSet(Window.SizeToContentProperty)) { if (double.IsNaN(window.Width)) { window.SizeToContent |= SizeToContent.Width; } if (double.IsNaN(window.Height)) { window.SizeToContent |= SizeToContent.Height; } } } window.Show(); return(window); }