当单独的项目时,MVVM显示来自VM的新窗口 [英] MVVM show new window from VM when seperated projects

查看:76
本文介绍了当单独的项目时,MVVM显示来自VM的新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题涉及创造了上千个主题的事物, 很抱歉,如果我忽略了我的问题的答案,但是就我看来,没有人真的可以回答我的问题.例如: 在MVVM WPF中打开新窗口

My question involves something where thousand and one topics are created about, and if I overlooked an answer to my question, I'm sorry, but as far as I looked none really could answer my question. For example: Opening new window in MVVM WPF

如果仅使用一个WPF项目(包括模型,vm和视图),答案是可以的,但是由于我正在学习如何以正确的方式实现MVVM(而且我已经读过多次,所以最佳实践是创建模型,视图模型和单独的gui项目的单独的类库(dll),在我看来似乎不起作用,因为如果我想创建类似IWindowService的接口(在先前的url上以及

The answers are okay when if you use just one WPF project (including models, vms and views) but since I'm learning how to implement MVVM the correct way (and I've read multiple times that best practice is to create seperate class lib (dll's) for model, viewmodel and a seperate gui project) that doesn't seem to work in my eyes because if I want to create an interface like IWindowService (described on previous url and also here, It's impossible to access Window or Control class because then I should have a reference to the gui project and the whole goal of the pattern is wrecked.

所以我的问题是如何在尊重松散耦合的MVVM原理和独立项目的同时,从MainViewModel中显示一个新窗口(带有新的视图模型).

So my question is how to show a new window (with a new viewmodel) from for example the MainViewModel, while respecting the loosely coupled MVVM principles AND seperate projects.

关于我要实现的目标的更深入的示例:

More in depth example of what I'm trying to achieve:

我具有以下结构:

模型(dll项目)
    个人资料

MODEL (dll project)
     Profile

VIEWMODEL (dll项目)
     MainViewModel
     AddProfileViewModel

VIEWMODEL (dll project)
     MainViewModel
     AddProfileViewModel

查看(WPF)(exe项目)
     MainWindow
     AddProfileWindow

VIEW (WPF) (exe project)
     MainWindow
     AddProfileWindow

我打开MainWindow,并想按AddProfile按钮,然后AddProfileWindow需要显示并带有附加的AddProfileViewModel.

I open MainWindow and I want to press the button AddProfile, then AddProfileWindow needs to show up with the AddProfileViewModel attached to it.

推荐答案

  • 在模型项目中定义IWindowService接口.
  • 从视图模型项目中引用模型项目.
  • 在WPF应用程序(视图)项目中实现IWindowService.
    • Define the IWindowService interface in the model project.
    • Reference the model project from the view model project.
    • Implement the IWindowService in the WPF application (view) project.
    • 然后应将按钮绑定到使用IWindowService打开窗口的ICommand.像这样:

      The button should then be bound to an ICommand that uses IWindowService to open the window. Something like this:

      public class MainWindowViewModel
      {
          private readonly IWindowService _windowService;
      
          public MainWindowViewModel(IWindowService windowService)
          {
              _windowService = windowService;
              AddProfile = new DelegateCommand(() =>
              {
                  _windowService.OpenProfileWindow(new AddProfileViewModel());
              });
          }
      
          public ICommand AddProfile { get; }
      }
      
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              DataContext = new MainWindowViewModel(new WindowService());
          }
      }
      
      public class WindowService : IWindowService
      {
          public void OpenProfileWindow(AddProfileViewModel vm)
          {
              AddProfileWindow win = new AddProfileWindow();
              win.DataContext = vm;
              win.Show();
          }
      }
      

      这篇关于当单独的项目时,MVVM显示来自VM的新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆