最好的初步实践在MVVM灯打开一个新窗口参数 [英] Best Pratice to open a New Window in MVVM Light with Parameters

查看:423
本文介绍了最好的初步实践在MVVM灯打开一个新窗口参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的MVVM和MVVM轻,但我想我明白它的总体思路。我不明白的是,如果我想打开一个新窗口,但窗口从调用的是这些数据得到了新窗口的最佳实践需要的数据?如果我的数据传递给构造函数则意味着我需要在代码后面的代码将它传递给视图模型。我不能使用消息,因为它不是基本数据。先谢谢了。

I am fairly new to mvvm and mvvm light, but I think I understand the general idea of it. What I don't understand is if I want to open a new window, but that window needs data from the caller what is the best practice to get that data to the new window? If I pass the data to the constructor then that means I need code in the code behind to pass it to the view model. I can't use messaging, because it isn't basic data. Thanks in advance.

推荐答案

一个流行的选择是使用,这将创建一个视图/视图模型服务类,并显示新视图。您的视图模型构造和/或方法/属性可以从呼叫者接收的数据,然后视图将被绑定到之前在屏幕上显示它的视图模型。

One popular choice is to use a service class that will create a view/viewmodel and display the new view. Your view model constructor and/or method/property could receive the data from the caller and then the view would be bound to the viewmodel prior to displaying it on the screen.

这里是一个非常非常简单的实现DialogService的:

here is a very very simple implementation of a DialogService:

public class DialogService : IDisposable
{
    #region Member Variables
    private static volatile DialogService instance;
    private static object syncroot = new object();
    #endregion

    #region Ctr
    private DialogService()
    {

    }
    #endregion

    #region Public Methods
    public void ShowDialog(object _callerContentOne, object _callerContentTwo)
    {
        MyDialogViewModel viewmodel = new MyDialogViewModel(_callerContentOne, _callerContentTwo);
        MyDialogView view = new MyDialogView();
        view.DataContext = viewmodel;

        view.ShowDialog();
    }
    #endregion

    #region Private Methods

    #endregion

    #region Properties
    public DialogService Instance
    {
        get
        {
            if (instance == null)
            {
                lock (syncroot)
                {
                    if (instance == null)
                    {
                        instance = new DialogService();
                    }
                }
            }
            return instance;
        }
    }
    #endregion
}

这篇关于最好的初步实践在MVVM灯打开一个新窗口参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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