简体   繁体   English

UserControl设计

[英]UserControl design

I have to write a new UserControl for AccountManagement. 我必须为AccountManagement编写一个新的UserControl To integrate in your application with several TabItem s my UserControl has to implement the IModule interface which looks something like: 为了与几个TabItem集成到您的应用程序中,我的UserControl必须实现IModule接口,该接口类似于:

public interface IModule : INotifyPropertyChanged
{
  FrameworkElement TabContent { get; }
  ImageSource TabIcon { get; }
  string TabTitle { get; }
}

I want to build the UserControl for AccountManagement with MVVM so I want to implement the IModule interface in my ViewModel. 我想使用MVVM构建用于AccountManagement的UserControl ,所以我想在ViewModel中实现IModule接口。

My two questions are: 我的两个问题是:

  1. Is this a good solution, or should I prefer to implement IModule in my View-CodeBehind? 这是一个好的解决方案,还是我更希望在View-CodeBehind中实现IModule

  2. If I implement IModule in the ViewModel, how can I pass the View as TabContent to the parent? 如果我在ViewModel中实现IModule ,如何将View作为TabContent传递给父级?

No. FrameworkElement is a view concept and should not be in the ViewModel. FrameworkElement是一个视图概念,不应位于ViewModel中。 I assume you will have to implement it in code-behind, but why don't you see how other tabs are written and ensure you are consistent with the existing codebase? 我假设您将不得不在后台代码中实现它,但是为什么不查看其他选项卡的编写方式并确保与现有代码库保持一致?

EDIT: You will still need to implement the above in the View. 编辑:您仍然需要在视图中实现以上内容。 All three properties are view based, with the possible exception of TabTitle which could pull its name from the view model. 这三个属性都是基于视图的, TabTitle可能是例外,它可以从视图模型中提取其名称。 There is nothing stopping you moving toward MVVM for the content of the tab, but this existing requirement will have to be done in the view. 对于选项卡的内容,没有什么可以阻止您转向MVVM的,但是必须在视图中完成此现有要求。

A good rule of thumb for deciding if something belongs in the VM is to consider whether you could unit test the VM without any view at all, or whether you could theoretically write a text based console view to drive the VM. 确定VM是否属于某个东西的一个很好的经验法则是考虑是否可以在没有任何视图的情况下对VM进行单元测试,或者理论上是否可以编写基于文本的控制台视图来驱动VM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM