UWP棱镜UWP视图模型



我一直在使用Prism的视图模型定位器,使用棱镜命名约定实例化我的视图模型。我之所以失败,是因为我用自己的视图模型创建了一个用户控件,我使用Prism事件聚合器与其他视图模型进行通信。这很好,但我想创建该用户控件的其他实例。使用事件聚合器,用户控件的所有实例显然都订阅了发布的事件,因此:

  1. 如何区分我想要针对的实际用户控件
  2. 如何在不使用视图模型定位器的情况下实例化视图模型并针对视图数据上下文

我所做的是在名为vmType的用户控件中创建一个依赖属性。

public string vmType
{
get { return (string)GetValue(vmTypeProperty); }
set { SetValue(vmTypeProperty, value); viewModel.vmType = vmType; }
}
// Using a DependencyProperty as the backing store for vmType.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty vmTypeProperty =
DependencyProperty.Register("vmType", typeof(string), typeof(UserControl), new PropertyMetadata(null));

在该集中,我将关联的视图模型属性vmType标记为用户控件应该用于的用途。在父视图的XAMl中,只需将vmType设置为所需的即可。

最新更新