UWP 棱镜 UWP 视图模型
UWP Prism UWP Viewmodel
我一直在使用 Prism 的 Viewmodel 定位器使用 prism 命名来实例化我的 viewmodels conventions.I 失败了,因为我创建了一个带有它自己的 viewmodel 的用户控件,我使用 Prism Event Aggregator 与其他 viewmodels 进行通信这工作得很好,但我想创建此用户控件的其他实例。使用事件聚合器,用户控件的所有实例显然都订阅了发布的事件:
- 如何区分我想要定位的实际用户控件。
- 如何在不使用视图模型定位器的情况下实例化视图模型并定位视图数据上下文?
我所做的是在名为 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 设置为您需要的即可。
我一直在使用 Prism 的 Viewmodel 定位器使用 prism 命名来实例化我的 viewmodels conventions.I 失败了,因为我创建了一个带有它自己的 viewmodel 的用户控件,我使用 Prism Event Aggregator 与其他 viewmodels 进行通信这工作得很好,但我想创建此用户控件的其他实例。使用事件聚合器,用户控件的所有实例显然都订阅了发布的事件:
- 如何区分我想要定位的实际用户控件。
- 如何在不使用视图模型定位器的情况下实例化视图模型并定位视图数据上下文?
我所做的是在名为 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 设置为您需要的即可。