AvaloniaRuntimeXamlLoader.cs 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Text;
  6. using Avalonia.Markup.Xaml.XamlIl;
  7. #nullable enable
  8. namespace Avalonia.Markup.Xaml
  9. {
  10. public static class AvaloniaRuntimeXamlLoader
  11. {
  12. /// <summary>
  13. /// Loads XAML from a string.
  14. /// </summary>
  15. /// <param name="xaml">The string containing the XAML.</param>
  16. /// <param name="localAssembly">Default assembly for clr-namespace:.</param>
  17. /// <param name="rootInstance">The optional instance into which the XAML should be loaded.</param>
  18. /// <param name="uri">The URI of the XAML being loaded.</param>
  19. /// <param name="designMode">Indicates whether the XAML is being loaded in design mode.</param>
  20. /// <returns>The loaded object.</returns>
  21. public static object Load(string xaml, Assembly? localAssembly = null, object? rootInstance = null, Uri? uri = null, bool designMode = false)
  22. {
  23. xaml = xaml ?? throw new ArgumentNullException(nameof(xaml));
  24. using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xaml)))
  25. {
  26. return Load(stream, localAssembly, rootInstance, uri, designMode);
  27. }
  28. }
  29. /// <summary>
  30. /// Loads XAML from a stream.
  31. /// </summary>
  32. /// <param name="stream">The stream containing the XAML.</param>
  33. /// <param name="localAssembly">Default assembly for clr-namespace:</param>
  34. /// <param name="rootInstance">The optional instance into which the XAML should be loaded.</param>
  35. /// <param name="uri">The URI of the XAML being loaded.</param>
  36. /// <param name="designMode">Indicates whether the XAML is being loaded in design mode.</param>
  37. /// <returns>The loaded object.</returns>
  38. public static object Load(Stream stream, Assembly? localAssembly = null, object? rootInstance = null, Uri? uri = null,
  39. bool designMode = false)
  40. => AvaloniaXamlIlRuntimeCompiler.Load(new RuntimeXamlLoaderDocument(uri, rootInstance, stream),
  41. new RuntimeXamlLoaderConfiguration { DesignMode = designMode, LocalAssembly = localAssembly });
  42. /// <summary>
  43. /// Loads XAML from a stream.
  44. /// </summary>
  45. /// <param name="stream">The stream containing the XAML.</param>
  46. /// <param name="configuration">Xaml loader configuration.</param>
  47. /// <returns>The loaded object.</returns>
  48. public static object Load(RuntimeXamlLoaderDocument document, RuntimeXamlLoaderConfiguration? configuration = null)
  49. => AvaloniaXamlIlRuntimeCompiler.Load(document, configuration ?? new RuntimeXamlLoaderConfiguration());
  50. /// <summary>
  51. /// Loads group of XAML files from a stream.
  52. /// </summary>
  53. /// <param name="documents">Collection of documents.</param>
  54. /// <param name="configuration">Xaml loader configuration.</param>
  55. /// <returns>The loaded objects per each input document.</returns>
  56. public static IReadOnlyList<object> LoadGroup(IReadOnlyCollection<RuntimeXamlLoaderDocument> documents, RuntimeXamlLoaderConfiguration? configuration = null)
  57. => AvaloniaXamlIlRuntimeCompiler.LoadGroup(documents, configuration ?? new RuntimeXamlLoaderConfiguration());
  58. /// <summary>
  59. /// Parse XAML from a string.
  60. /// </summary>
  61. /// <param name="xaml">The string containing the XAML.</param>
  62. /// <param name="localAssembly">Default assembly for clr-namespace:.</param>
  63. /// <returns>The loaded object.</returns>
  64. public static object Parse(string xaml, Assembly? localAssembly = null)
  65. => Load(xaml, localAssembly);
  66. /// <summary>
  67. /// Parse XAML from a string.
  68. /// </summary>
  69. /// <typeparam name="T">The type of the returned object.</typeparam>
  70. /// <param name="xaml">>The string containing the XAML.</param>
  71. /// <param name="localAssembly">>Default assembly for clr-namespace:.</param>
  72. /// <returns>The loaded object.</returns>
  73. public static T Parse<T>(string xaml, Assembly? localAssembly = null)
  74. => (T)Parse(xaml, localAssembly);
  75. }
  76. }