DesignerAssist.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using Avalonia.Controls;
  9. using Avalonia.Controls.Platform;
  10. using Avalonia.Markup.Xaml;
  11. using Avalonia.Platform;
  12. using Avalonia.Styling;
  13. using Avalonia.Themes.Default;
  14. namespace Avalonia.DesignerSupport
  15. {
  16. class DesignerAssist
  17. {
  18. class DesignerApp : Application
  19. {
  20. public override void Initialize()
  21. {
  22. Styles.Add(new DefaultTheme());
  23. var loader = new AvaloniaXamlLoader();
  24. var baseLight = (IStyle)loader.Load(
  25. new Uri("resm:Avalonia.Themes.Default.Accents.BaseLight.xaml?assembly=Avalonia.Themes.Default"), null);
  26. Styles.Add(baseLight);
  27. }
  28. }
  29. public static DesignerApi Api { get; set; }
  30. public static void Init(Dictionary<string, object> shared)
  31. {
  32. Design.IsDesignMode = true;
  33. Api = new DesignerApi(shared) {UpdateXaml = UpdateXaml, UpdateXaml2 = UpdateXaml2, SetScalingFactor = SetScalingFactor};
  34. var runtimeAssembly = Assembly.Load(new AssemblyName("Avalonia.DotNetFrameworkRuntime"));
  35. var plat = (IRuntimePlatform) Activator.CreateInstance(runtimeAssembly
  36. .DefinedTypes.First(typeof (IRuntimePlatform).GetTypeInfo().IsAssignableFrom).AsType());
  37. TypeInfo app = null;
  38. var asms = plat.GetLoadedAssemblies();
  39. foreach (var asm in asms)
  40. {
  41. if(Equals(asm, typeof(Application).GetTypeInfo().Assembly) || Equals(asm, typeof(DesignerApp).GetTypeInfo().Assembly))
  42. continue;
  43. try
  44. {
  45. app = asm.DefinedTypes.Where(typeof (Application).GetTypeInfo().IsAssignableFrom).FirstOrDefault();
  46. if (app != null)
  47. break;
  48. }
  49. catch
  50. {
  51. //Ignore, Assembly.DefinedTypes threw an exception, we can't do anything about that
  52. }
  53. }
  54. var builderType = runtimeAssembly.GetType("Avalonia.AppBuilder");
  55. var builder = (dynamic)Activator.CreateInstance(builderType,
  56. app == null ? new DesignerApp() : (Application) Activator.CreateInstance(app.AsType()));
  57. builder
  58. .UseWindowingSubsystem("Avalonia.Win32")
  59. .UseRenderingSubsystem("Avalonia.Direct2D1")
  60. .SetupWithoutStarting();
  61. }
  62. private static void SetScalingFactor(double factor)
  63. {
  64. PlatformManager.SetDesignerScalingFactor(factor);
  65. s_currentWindow?.PlatformImpl?.Resize(s_currentWindow.ClientSize);
  66. }
  67. static Window s_currentWindow;
  68. private static void UpdateXaml(string xaml) => UpdateXaml2(new DesignerApiXamlFileInfo
  69. {
  70. Xaml = xaml
  71. }.Dictionary);
  72. private static void UpdateXaml2(Dictionary<string, object> dic)
  73. {
  74. var xamlInfo = new DesignerApiXamlFileInfo(dic);
  75. Window window = DesignWindowLoader.LoadDesignerWindow(xamlInfo.Xaml, xamlInfo.AssemblyPath);
  76. s_currentWindow?.Close();
  77. s_currentWindow = window;
  78. // ReSharper disable once PossibleNullReferenceException
  79. // Always not null at this point
  80. Api.OnWindowCreated?.Invoke(window.PlatformImpl.Handle.Handle);
  81. Api.OnResize?.Invoke();
  82. }
  83. }
  84. }