MVVM 在分离项目时显示来自 VM 的新窗口 [英] MVVM show new window from VM when seperated projects

查看:17
本文介绍了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 项目(包括模型、虚拟机和视图),答案是可以的,但由于我正在学习如何以正确的方式实现 MVVM(并且我已经多次阅读最佳实践是创建用于模型、视图模型和单独的 gui 项目的单独类库(dll)在我看来似乎不起作用,因为如果我想创建一个像 IWindowService 这样的接口(在前面的 url 中描述,还有 这里,不可能访问 Window 或 Control 类,因为那样我应该有一个对 gui 项目的引用,并且整个模式的目标都被破坏了.

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.

所以我的问题是如何显示来自例如 MainViewModel 的新窗口(带有新的视图模型),同时尊重松散耦合的 MVVM 原则和单独的项目.

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

VIEW (WPF)(exe 项目)
     主窗口
     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.
  • 然后按钮应该绑定到使用 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天全站免登陆