【问题标题】:Create View from ViewModel object从 ViewModel 对象创建视图
【发布时间】:2014-08-28 04:17:36
【问题描述】:

我看到 MvvmCross touch 支持使用 MvxViewModelRequest 从 ViewModel 对象创建视图。

但在 MvvmCross WPF 中,我只能使用 MvxViewModelRequest 创建视图

Mvx.Resolve<IMvxSimpleWpfViewLoader>().CreateView(viewmodelRequest)

但是,我找不到从 ViewModel 对象创建视图的方法? MvvmCross 是否支持 WPF?

【问题讨论】:

    标签: wpf view mvvmcross viewmodel


    【解决方案1】:

    默认情况下 Wpf 中不包含此功能 - 但您可以轻松添加它。

    逻辑类似于https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Wpf/Views/MvxWpfViewsContainer.cs 中基于请求的代码 - 类似于:

      // Use `IMvxViewFinder` to find the type of view: 
      var viewType = Mvx.Resolve<IMvxViewFinder>().GetViewType(myViewModel.GetType());
    
      // create a view and set the data context
      var viewObject = Activator.CreateInstance(viewType);
      if (viewObject == null)
         throw new MvxException("View not loaded for " + viewType);
    
      var wpfView = viewObject as IMvxWpfView;
      if (wpfView == null)
         throw new MvxException("Loaded View does not have IMvxWpfView interface " + viewType);
    
      wpfView.ViewModel = myViewModel;
    

    如果您愿意,您可以将其构建到自定义视图容器或自定义视图呈现器中。

    【讨论】:

      【解决方案2】:

      假设你有

      public partial class LoginViewController : MvxViewController<LoginViewModel>
      

      比,如果我想在某个地方使用视图,你可以做类似的事情

      this.presentedCurrentController = Activator.CreateInstance(typeof(LoginViewController)) as LoginViewController;
      (this.presentedCurrentController as LoginViewController).ViewModel = new LoginViewModel();
      

      在哪里

      this.presentedCurrentController it's
      
      var NSViewController presentedCurrentController;
      

      也感谢@cheesebaron 提供linkanother one

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-27
        • 1970-01-01
        • 1970-01-01
        • 2011-05-17
        • 2010-10-27
        • 2013-10-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多