使用MVVMLight将变量传递给第二个窗口?

时间:2017-10-10 01:02:47

标签: c# wpf mvvm-light

我正在构建一个应用程序来自学MVVM和一些谷歌搜索(以及一些试验错误)我已经设法达到我可以从ViewModel打开第二个窗口但不从一个页面传递变量的程度到另一个。这是我的ViewModel。

    public VendorSelectViewModel()
    {
        Ping ping = new Ping();
        PingReply pingresult = ping.Send("192.168.1.10");
        if (pingresult.Status.ToString() == "Success")
        {
            LoadVendorsAsync();
        }
        else
        {
            LoadVendors();
        }

        NextCommand = new RelayCommand(NextWindow);
    }

    public ICommand NextCommand { get; private set; }

    void NextWindow()
    {
        Console.WriteLine(selectedVendor.VendorName); 
        Messenger.Default.Send(new NotificationMessage("NextWindow"));
    }

在我看来,我有这个

    public VendorSelectWindow()
    {
        InitializeComponent();
        _vm = new Biz.Invoicer.VendorSelectViewModel();
        DataContext = _vm;

        Messenger.Default.Register<NotificationMessage>(this, NotificationMessageReceived);
    }

    private void NotificationMessageReceived(NotificationMessage msg)
    {
        if (msg.Notification == "NextWindow")
        {
            var invoicerWindow = new InvoicerWindow();
            invoicerWindow.Show();
        }
    }

所以我知道(或者我想我知道)这可能不是一个“最佳实践”,但我将回到这个并重构,因为我更好地了解MVVM模式和MVVM Light。目前我正在尝试将第一页的ViewModel(VendorSelectViewModel)中的变量传递给第二页(InvoicerWindow),但我还没有设法正确的语法。

将变量从一个页面传递到下一个页面需要做什么?

2 个答案:

答案 0 :(得分:2)

首先,您可以传递任意对象作为IMessenger.Send<TMessage>方法的参数 - TMessage类型参数不受限制。 E.g:

//ViewModel:
void NextWindow()
{
    //...
    int someValue = 10;
    Messenger.Default.Send(someValue);
}

//View:
public VendorSelectWindow()
{
    //...
    Messenger.Default.Register<int>(this, MessageReceived);
}

private void MessageReceived(int value)
{
    //...
}

但是,如果您发现NotificationMessage类在您的情况下特别有用,则可以使用通用NotificationMessage<T>版本,该版本会公开任意类型Content的其他属性T

//ViewModel:
void NextWindow()
{
    //...
    int someValue = 10;
    Messenger.Default.Send(new NotificationMessage<int>(someValue, "Notification text"));
}

//View:
public VendorSelectWindow()
{
    //...
    Messenger.Default.Register<NotificationMessage<int>>(this, MessageReceived);
}

private void MessageReceived(NotificationMessage<int> message)
{
    var someValue = message.Content;
    //...
}

或者,如果这不适合您,您可以创建自己的类,派生自NotificationMessage并公开其他成员并将其用作消息对象。

答案 1 :(得分:1)

您可以传递自己的自定义类型的实例,而不是将NotificationMessage传递给信使,该实例可以包含您想要的任意数量的值:

void NextWindow()
{
    Console.WriteLine(selectedVendor.VendorName); 
    Messenger.Default.Send(new YourPayload() {WindowName = "NextWindow", Parameter = "some value..:");
}
...

public VendorSelectWindow()
{
    InitializeComponent();
    _vm = new Biz.Invoicer.VendorSelectViewModel();
    DataContext = _vm;

    Messenger.Default.Register<YourPayload>(this, NotificationMessageReceived);
}

private void NotificationMessageReceived(YourPayload msg)
{
    if (msg.WindowName == "NextWindow")
    {
        string param = msg.Parameter;
        var invoicerWindow = new InvoicerWindow();
        invoicerWindow.Show();
    }
}

YourPayload是一个自定义类,包含两个属性WindowNameParameter