2011-02-17 4 views
0

프로젝트를 개발 중이고 viewModel과 View 간의 통신에 중재자 패턴을 사용하고 있습니다.MVVM light, 조정자 패턴의 메시지는 수동으로 삭제해야합니까?

문제는 메시지만큼 등록 된 메서드가 메시지만큼 전송된다는 것입니다.

음, 내 문제를 적어 두십시오. 간단한 메뉴에서

나는 항목을 가지고하는 I는 그것을 내가 간단한 계산을했다 RoomPricesViewModel에서 명령

//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"); 
    } 

을 할당하고 나는 다른 하나를 엽니 다 나중에보기를 닫고해야합니다.

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; 
      } 
     } 
    } 

코드가 작동하는 것 같다하지만이 버튼을 클릭하면, 뷰는이 메시지 박스 나에게 묻습니다, 개방 내가 예를 클릭하면 현재보기를 닫고 다른 하나는 개방된다.

다시 버튼을 클릭하면 창이 닫히고 하나 대신 두 개의 창이 열립니다. 그것을 10 번을 클릭하면

, 당신이 상상할 수있는 :)

나는이 어떻게

막을 수 ? 어떻게 든 메시지를 죽여야 만합니까?

메시지 작성이 매우 어려워서 메시징 (중재자 패턴)과 많이 혼동되어 있지만 익숙해지면 상황이 훨씬 쉬워 질 것입니다.

나는 도움이나 조언을 부탁드립니다.

감사

답변

0

솔루션

문제는 그 메시지와 여러 번이 창에 메시지를 등록 된 너무 많은 시간을 연 윈도우의 생성자에 등록 된 것이 었습니다. 따라서 "NotificationMessage"가 등록 된 횟수만큼 창이 열렸습니다.

나는 간단하게 실행이

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

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

및 문제 해결.

관련 문제