Set d:DataContext to an internal class of another assembly

别等时光非礼了梦想. 提交于 2020-06-17 03:40:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!