Normally you assign resources in XAML by using the
The static behavior is simple. Just find the resource by using the
The BAML API is something unique of WPF and was not public until .NET 4.0. Now it's available through the
Load a BAML stream in .NET 4.0:
StaticResource
or DynamicResource
extension. But how to do it programmatically?The static behavior is simple. Just find the resource by using the
TryFindResource()
method and set it to the property of your choice. But if you want to have it dynamically updated, the following code snipped is your solutions:frameworkElement.SetResourceReference(dependencyProperty, resourceKey);
How to load BAML resources
What is BAML?
WPF is based on XAML an XML dialect to describe object graphs. This is very powerful but parsing an XAML file at runtime is quite expensive. So theMarkupCompiler
converts the XAML file to a more compact binary version called BAML. The BAML stream then is stored to the resources of the assembly and loaded within the InitializeComponent
method.The BAML API is something unique of WPF and was not public until .NET 4.0. Now it's available through the
Baml2006Reader
implementation. The following code shows how to load a BAML stream in .NET 3.5 and 4.0Load an object from a BAML stream
Load a BAML stream in .NET 3.5 (by using reflection):var pc = new ParserContext(); var readerType = presentationFrameworkAssembly .GetType("System.Windows.Markup.XamlReader"); var method = readerType.GetMethod("LoadBaml", BindingFlags.NonPublic | BindingFlags.Static); return method.Invoke(null, new object[] {stream, pc, null, false});
No comments:
Post a Comment