问题
I am trying to use a ResourceDictionary in loose XAML and load it a runtime to provide templates and styles to a WPF app. I have the XAML available in a local directory and am adding a new ResourceDictionary to App.Current.Resources.MergedDictionaries in app startup using a URI.
When the XAML goes to parse, it blows up on a template where the TargetType is a custom control from the assembly that is consuming it.
The specific message is:
'Failed to create a 'Type' from the text 'controls:CustomType'.'
I already have the namespace mapped in the ResourceDictionary at the top:
xmlns:controls="clr-namespace:TEST.UI.WPF.Common.Controls"
There are articles out there stating that loading ResourceDictionaries from loose XAML is possible but none of them that I have found address custom types within those loose XAML files.
Any help is appreciated!
回答1:
Since you are loading this at runtime and it is not built within the project then the ResourceDictionary does not know of your assembly. You will have to include the assembly in the namespace for it to be recognised.
xmlns:controls="clr-namespace:TEST.UI.WPF.Common.Controls;assembly=Common"
You should be able to run you app after you add ';assembly=yourAssembly' to the namespace declaration.
来源:https://stackoverflow.com/questions/21465443/wpf-loose-xaml-resourcedictionary