在我的应用程序中,我需要一组不同的 I/O 设备,例如串行、OPC、USB 设备等。我使用 MEF 框架提供了一种处理集合并允许添加新设备的方法。
我的项目看起来像这样:
Atf.Model 'contains the model
Atf.Gui 'the views and view-models
Atf.Devices 'contains implementations of IDevice and exports them
许多设备需要配置,因此设备接口公开了自定义控件的路径和处理设备配置编辑的相应视图模型。我试图坚持 MVVM 模式,并希望尽可能地分离视图和模型。同时我想保持与设备集合的耦合尽可能松散。
在Atf.Gui
我有一个控件,它显示所有发现的设备并显示激活的设备。选择激活的设备后,我想动态显示其编辑器。
我该怎么做?这里有一些(疯狂的)想法:
Idea-1UserControl
只需使用设备对象中的路径在我的视图模型中加载一个。这将打破 MVVM 分离并使该部分“不可测试”
public System.Windows.Controls.UserControl ConfigureControl
{
get
{
// code to load the UserControl using the path property
}
}
Idea-2让设备只公开一个视图模型并使用映射(在设备存储库中定义)来获取视图。不知道如何做到这一点。
<myeditorcontainer>
<ContainerControl Content="{Binding CurrentlySelectedDeviceViewModel}"/>
</myeditorcontainer>
Idea-3在我看来,使用绑定加载控件。不确定这是否可能。
<myeditorcontainer>
<UserControl Path="{Binding CurrentlySelectedDeviceViewPath}"
DataContext="{Binding CurrentlySelectedDeviceViewModel}"/>
</myeditorcontainer>