3

私は自分のアプリでmvvmlightツールキットを使用しています。ビューモデルのメッセージボックスを使用したい。だから:Messenger.Default.Register(App.xaml.csに)を登録できますか?すべてのビューモデルに登録する必要があります。すべてのViewModelに登録したくありません。Messenger.Default.Unregister()また、非アクティブ化または終了イベントを呼び出すことはできますか?

ありがとう

4

1 に答える 1

2

MVVMとメッセージボックスの1つの可能なアプローチは、単純なイベントメカニズムです。

public class MessageBoxDisplayEventArgs : EventArgs
{
    public string Title { get; set; }

    // Other properties here...
}
...
public class ViewModelBase
{
    public event EventHandler<MessageBoxDisplayEventArgs> MessageBoxDisplayRequested;

    protected void OnMessageBoxDisplayRequest(string title)
    {
        if (this.MessageBoxDisplayRequested != null)
        {
            this.MessageBoxDisplayRequested(
                this, 
                new MessageBoxDisplayEventArgs
                {
                    Title = title
                });
        }
    }
}
...
public class YourViewModel : ViewModelBase
{
    private void SomeMethod()
    {
        this.OnMessageBoxDisplayRequest("hello world");
    }
}
...
public class YourView
{
    public YourView()
    {
        var vm = new YourViewModel();
        this.Datacontext = vm;

        vm.MessageBoxDisplayRequested += (sender, e) =>
        {
            // UI logic here
            //MessageBox.Show(e.Title);
        };
    }
}
于 2013-03-14T13:26:20.143 に答える