RuntimeXamlLoaderDocument and ConfigurationLoadGroup)Primary APIs:
AvaloniaXamlLoader.Load(object)AvaloniaXamlLoader.Load(IServiceProvider?, Uri, Uri?)AvaloniaRuntimeXamlLoader.Load(...)AvaloniaRuntimeXamlLoader.LoadGroup(...)AvaloniaRuntimeXamlLoader.Parse(...)RuntimeXamlLoaderDocumentRuntimeXamlLoaderConfigurationRuntimeXamlDiagnosticRuntimeXamlDiagnosticSeverityReference source files:
src/Markup/Avalonia.Markup.Xaml/AvaloniaXamlLoader.cssrc/Markup/Avalonia.Markup.Xaml.Loader/AvaloniaRuntimeXamlLoader.cssrc/Markup/Avalonia.Markup.Xaml/RuntimeXamlLoaderDocument.cssrc/Markup/Avalonia.Markup.Xaml/RuntimeXamlLoaderConfiguration.cssrc/Markup/Avalonia.Markup.Xaml.Loader/AvaloniaXamlIlRuntimeCompiler.cssrc/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlDiagnosticCodes.csInitializeComponent() calling AvaloniaXamlLoader.Load(this).If no compiled XAML exists for the type, AvaloniaXamlLoader.Load(this) throws XamlLoadException.
AvaloniaXamlLoader.Load(Uri, ...) tries:
CompiledAvaloniaXaml.!XamlLoader.TryLoad(...),IRuntimeXamlLoader) if registered.The runtime fallback path is primarily intended for tests/infrastructure, not normal app startup.
AvaloniaRuntimeXamlLoader compiles and loads XAML at runtime from:
RuntimeXamlLoaderDocument,LoadGroup).RuntimeXamlLoaderDocument and ConfigurationRuntimeXamlLoaderDocument supports:
BaseUri for URI resolution,Document path for diagnostics/source info,RootInstance to populate an existing instance,XamlStream,ServiceProvider parent provider.RuntimeXamlLoaderConfiguration controls:
LocalAssembly for clr-namespace: lookup,UseCompiledBindingsByDefault,DesignMode,CreateSourceInfo,DiagnosticHandler severity override callback.Example:
using Avalonia.Markup.Xaml;
var doc = new RuntimeXamlLoaderDocument(
new Uri("avares://MyPlugin/Views/EditorView.axaml"),
xamlText)
{
Document = "/plugins/editor/EditorView.axaml"
};
var view = (object)AvaloniaRuntimeXamlLoader.Load(
doc,
new RuntimeXamlLoaderConfiguration
{
LocalAssembly = typeof(MyPluginMarker).Assembly,
UseCompiledBindingsByDefault = true,
CreateSourceInfo = true
});
LoadGroup)LoadGroup resolves references among in-memory documents (including relative Source paths), which is useful for plugin packs or hot reload batches.
using Avalonia.Markup.Xaml;
var docs = new[]
{
new RuntimeXamlLoaderDocument(new Uri("avares://Plugin/Styles/Base.axaml"), styleXaml),
new RuntimeXamlLoaderDocument(new Uri("avares://Plugin/Views/Main.axaml"), viewXaml)
};
var loaded = AvaloniaRuntimeXamlLoader.LoadGroup(docs);
// loaded[i] aligns with docs[i]
Runtime diagnostics are surfaced through DiagnosticHandler.
var diagnostics = new List<RuntimeXamlDiagnostic>();
var cfg = new RuntimeXamlLoaderConfiguration
{
DiagnosticHandler = d =>
{
diagnostics.Add(d);
// Escalate warnings to errors in strict mode.
if (d.Severity == RuntimeXamlDiagnosticSeverity.Warning)
return RuntimeXamlDiagnosticSeverity.Error;
return d.Severity;
}
};
Diagnostic IDs map to Avalonia codes (AVLNxxxx) from compiler diagnostics.
Dynamic loader APIs are annotated with RequiresUnreferencedCode and should be treated as opt-in runtime features.
Risky paths for trim/AOT:
Prefer compiled XAML + compiled bindings for startup and core UX surfaces.
var existing = new MyUserControl();
var doc = new RuntimeXamlLoaderDocument(rootInstance: existing, xaml: xamlText);
AvaloniaRuntimeXamlLoader.Load(doc);
var style = AvaloniaRuntimeXamlLoader.Parse<Avalonia.Styling.Style>(xamlText);
Set BaseUri on each document when using relative Source in includes.
XamlLoadException from AvaloniaXamlLoader.Load(this)
clr-namespace type not found
RuntimeXamlLoaderConfiguration.LocalAssembly.RuntimeXamlLoaderDocument.BaseUri.UseCompiledBindingsByDefault, DesignMode, and document group context.