【问题标题】:UWP Prism UWP ViewmodelUWP 棱镜 UWP 视图模型
【发布时间】:2019-06-12 00:38:18
【问题描述】:

我一直在使用 Prism 的 Viewmodel 定位器来使用 prism 命名约定来实例化我的视图模型。我遇到了困难,因为我用它自己的视图模型创建了一个用户控件,我使用 Prism 事件聚合器与其他视图模型通信,这很好用但我想创建此用户控件的其他实例。使用事件聚合器,用户控件的所有实例显然都订阅​​了已发布的事件,因此:

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

【问题讨论】:

    标签: c# xaml uwp prism


    【解决方案1】:

    我所做的是在名为 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 设置为您所需要的即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多