开发者

MVVM light, Messages from mediator pattern should be killed manually?

开发者 https://www.devze.com 2023-02-11 06:50 出处:网络
I am developing a project and I am using the mediator pattern for communication between viewModel and View.

I am developing a project and I am using the mediator pattern for communication between viewModel and View.

The problem is that, the method that is registered with a message executed as many times as the message is sent.

well, lets write down my problem.

From a simple menu I have an item an I have assign to it a command

  //MainWindow.xaml
<awc:ImageButton IsToolStyle="True"  Orientation="Vertical" ImageSource="" Command="{Binding ShowPricesWindowCommand}">Prices</awc:ImageButton>


 //MainWIndow ViewModel
 public ICommand ShowPricesWindowCommand {
        get { return new RelayCommand(ShowPricesWindowExecute); }
    }
void ShowPricesWindowExecute() {
        Messenger.Default.Send(new NotificationMessage<Hotel>(this, SelectedHotel, "ShowPricesWindow"),
                               "ShowPricesWindow");
    }


//MainWindow.xaml.cs
Messenger.Default.Register<NotificationMessage<Hotel>>(this, "ShowPricesWindow", HotelPriceMessageReceived);

private void HotelPriceMessageReceived(NotificationMessage<Hotel> selectedHotel) {
        var roomPrices = new RoomPrices();//This view has the RoomPriceViewModel as dataContext
        roomPrices.Show();
        //via messaging I am sending the selectedHotel object
        Messenger.Default.Send(new NotificationMessage<Hotel>(this, selectedHotel.Content, "ShowPricesWindow"),
                               "ShowPricesWindow2");
    }

From RoomPricesViewModel I made a simple calculation and I need to close the view and afterwards to open an other one.

public RoomPricesViewModel(IDialogService dialogService) {
        this._dialog = dialogService;
        Messenger.Default.Register<NotificationMessage<Hotel>>(this, "ShowPricesWindow2", NotificationMessageReceived);
    }

    private void NotificationMessageReceived(NotificationMessage<Hotel> selectedHotel) {
        this.SelectedHotel = selectedHotel.Content;
        LoadRooms();
    }

void LoadRooms() {
        if (rooms.Count == 0) {
            dialogResponse = _dialog.ShowMessage("Display a message;", "", DialogButton.YesNo, DialogImage.Warning);
            switch (dialogResponse) {
                case DialogResponse.Yes:
                    //close the RoomPrices window and open the RoomTypesWindow
                    Messenger.Default.Send(new NotificationMessage<Hotel>(this, this.SelectedHotel, "CloseWindowAndOpenRoomTypes"), "CloseWindowAndOpenRoomTypes");
                    return;
                    break;
                case DialogResponse.No:
                    break;
            }
        }
    }

The code seems to work but If I click on the button, a view is opening, it prompts me with a messagebox and If I click yes, the current view closed and another one is opening.

If I click again the button the window is closed and two windows are opened instead of one.

If click it 10 times, you can imagine :)

How could I prevent this? Must I kill somehow the message?

It seems that it is very bad written, I have been confused a lot with messaging(mediator pattern) but I开发者_运维问答 know that if I get used to it, things will be much easier.

I will appreciate any help or advise.

Thanks


Solution

The problem was that I registered in the constructor of the window the message and as many times the window opened so many times the messages was registered. Hence, the window opened as many times the "NotificationMessage" was registered.

I simple run this

public RoomPricesViewModel(IDialogService dialogService) {
        Messenger.Default.Register<NotificationMessage<Hotel>>(this, "ShowPricesWindow", NotificationMessageReceived);
    }

    private void NotificationMessageReceived(NotificationMessage<Hotel> selectedHotel) {
        //code
        Messenger.Default.Unregister(this);
    }

and the problem solved.

0

精彩评论

暂无评论...
验证码 换一张
取 消