Can't find types in ResourceDictionary when loading with XamlReader.Load
In my prism module, I have the following code snippet:
using (var manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyAssembly.CaseHost.ViewModelDataTemplates.xaml"))
{
ParserContext context = new ParserContext();
context.XmlnsDictionary.Add("local", "clr-namespace:MyAssembly.CaseHost");
var resourceDictionary = (ResourceDictionary)XamlReader.Load(manifestResourceStream, context)开发者_运维百科;
_resourceRegistry.Add(resourceDictionary);
}
I'm basically trying to load up this very simple ResourceDictionary:
<DataTemplate DataType="{x:Type local:PlayPauseViewModel}">
<Label>Look mom!</Label>
</DataTemplate>
This gives me the following exception:
Type reference cannot find public type named 'PlayPauseViewModel'.
This is a cpp/winforms/wpf application beast, so I can't use URI's. How can I solve this?
Found it!
The problem was how the namespace was specified in the DataTemplate:
Standard definition (did not work):
xmlns:local="clr-namespace:<Namespace>"
More explicit definition (worked!)
xmlns:local="clr-namespace:<Namespace>;assembly=<assembly>"
Replace <> with namespace and assembly.
But what to do if you have this code in the same assembly? You are not allowed to use
xmlns:local="clr-namespace:<Namespace>;assembly=<assembly>"
there?
精彩评论