问题
Consider the following scenario:
WPF view:
d:DataContext="{d:DesignInstance vm:DesignViewModel, IsDesignTimeCreatable=True}"
ViewModel is located in a separate c# project:
internal class DesignViewModel : ViewModel
{ /* create demo data in ctor */ }
ViewModel has this entry in AssemblyInfo:
[assembly: InternalsVisibleTo("WPF.Demo.View")]
Error message in XAML Designer:
- The name "DesignViewModel" does not exist in the namespace "WPF.Demo.View".
Observations:
- Everything works fine if I make DesignViewModel public, instead of internal. But, I'd like to keep it internal, for external consumers really shouldn't bother about design time stuff.
- Everything works fine if I move DesignViewModel into the same assembly that holds the view. But, I'd like to keep view and ViewModel separate.
- InternalsVisibleTo is set correctly, for I can access it from code behind in the view.
Question: How can I set d:DataContext to an internal class of another assembly?
回答1:
You can't. By definition, when you mark an artifact as internal you are making it invisible from other assemblies.
Alternatively, you can expose a method on a public object, that makes what you want in the assembly's objects.
Regards,
来源:https://stackoverflow.com/questions/54178579/set-ddatacontext-to-an-internal-class-of-another-assembly