Galasoft Messenger显示窗口

时间:2011-12-21 15:23:06

标签: wpf mvvm mvvm-light

我想从我的ViewModel打开一个窗口。 如何使用Galasoft Messenger创建和显示它?

public partial class View {
    public View() {
        InitializeComponents();
        //Register Open message
    }

    //This is called when ViewModel sends a message
    public void OpenView() {
        new View().Show();
    }
}

public class ViewModel {
    public ViewModel() {
        //Send message to open some view
    }
}

1 个答案:

答案 0 :(得分:2)

这种情况不需要将对象从ViewModel传递给View;因此,只需注册类型对象,传递null,但令牌是关键。

public partial class View {
    public View() {
        InitializeComponents();
        //Register Open message BEFORE ViewModel calls Messenger.Default.Send
        Messenger.Default.Register<object>(this, ViewModel.OpenViewToken, p => { OpenView(); });
    }

    //This is called when ViewModel sends a message
    public void OpenView() {
        new View().Show();
    }
}

public class ViewModel {
    public static readonly Guid OpenViewToken = Guid.NewGuid();
    public ViewModel() {
        Messenger.Default.Send<object>(null, OpenViewToken);
    }
}