2016-09-26 13 views
0

몇 가지 조언이 필요합니다. 누구든지 MVVM Light의 Messenger를 Prims로 변환하는 방법을 알고 있습니다.WPF MVVM 라이트 메신저 및 프리즘 EventAggregator

MVVM 빛

Messenger.Default.Send(new NavigateToPageMessage() 
       { 
        Page = "/MasterDetail.DetaiPage" 
       }); 
    private void InitializeMessageInterception() 
    { 
     MessengerInstance.Register<NavigateToPageMessage>(this, OnNavigateToPage); 
    } 

프리즘

this.eventAggregator.GetEvent<PubSubEvent<NavigateToPageMessage>>().Subscribe(parametr => 
      { 
       parametr.Page = "/MasterDetail.DetaiPage"; 
      }); 
    private void InitializeMessageInterception() 
    { 
     this.eventAggregator.GetEvent<PubSubEvent<NavigateToPageMessage>>().Subscribe(OnNavigateToPage); 
    } 

MVVM 빛의 전체 코드 :

나는 프리즘에 MVVM 라이트에서이 코드를 변환하는 문제가

/// <summary> 
/// Uses for navigation to page 
/// </summary> 
public class NavigateToPageMessage 
{ 
    /// <summary> 
    /// Page 
    /// </summary> 
    public string Page { get; set; } 

    /// <summary> 
    /// Params 
    /// </summary> 
    public Dictionary<string, object> Parameters { get; set; } 
} 

    private readonly MenuItemsCollection _mainMenuItems = new MenuItemsCollection() 
    { 

     new MainMenuItem() {Group = "MainGroup",Page = "/Main.Page1", Title = "Page1"}, 
     new MainMenuItem() {Group = "MainGroup",Page = "/Main.Page2", Title = "Page2"}, 
     new MainMenuItem() {Group = "MainGroup",Page = "/Main.Page3", Title = "Page3"}, 

    }; 
    private int _selectedMainMenuItemIndex; 
    private bool _canNavigate = true; 
    private MainMenuItem _selectedMainMenuItem; 

    public RelayCommand<string> GoToPageCommand { get; private set; } 
    public RelayCommand GoBackCommand { get; private set; } 


    /// <summary> 
    /// Initializes a new instance of the MainViewModel class. 
    /// </summary> 
    public MainViewModel()//IDataService dataService 
    { 
     InitializeCommands(); 
     InitializeMessageInterception(); 
    } 
    private void InitializeCommands() 
    { 

     GoToPageCommand = new RelayCommand<string>(page => OnNavigateToPage(new NavigateToPageMessage() { Page = page })); 
     GoBackCommand = new RelayCommand(() => 
     { 
      var frame = Application.Current.MainWindow.GetVisualDescendents().OfType<Frame>().FirstOrDefault(f => f.Name == "RootFrame"); 
      if (frame == null) 
       return; 

      if (frame.CanGoBack) 
       frame.GoBack(); 

      UpdateCanGoBack(); 
     }); 

    } 
    private void InitializeMessageInterception() 
    { 
     MessengerInstance.Register<NavigateToPageMessage>(this, OnNavigateToPage); 
    } 
    public MenuItemsCollection MainMenuItems 
    { 
     get { return _mainMenuItems; } 
    } 
    public void UpdateCanGoBack() 
    { 
     RaisePropertyChanged("CanGoBack"); 


     var frame = Application.Current.MainWindow.GetVisualDescendents().OfType<Frame>().FirstOrDefault(f => f.Name == "RootFrame"); 
     if (frame != null && frame.Content != null) 
     { 
      var source = frame.Content.GetType().Name; 
      _canNavigate = false; 
      SelectedMainMenuItemIndex = _mainMenuItems.IndexOf(_mainMenuItems.FirstOrDefault(t => t.Page.EndsWith(source))); 
      _canNavigate = true; 
     } 
    } 
    public bool CanGoBack 
    { 
     get 
     { 
      var frame = Application.Current.MainWindow.GetVisualDescendents().OfType<Frame>().FirstOrDefault(); 
      if (frame == null) 
       return false; 
      return frame.CanGoBack; 
     } 
    } 
    public int SelectedMainMenuItemIndex 
    { 
     get 
     { 
      return _selectedMainMenuItemIndex; 
     } 
     set 
     { 
      Set(ref _selectedMainMenuItemIndex, value); 
     } 
    } 
    public MainMenuItem SelectedMainMenuItem 
    { 
     get { return _selectedMainMenuItem; } 
     set 
     { 
      if (Set(ref _selectedMainMenuItem, value) && value != null && _canNavigate) 
       OnNavigateToPage(new NavigateToPageMessage() { Page = value.Page }); 
     } 
    } 
    private void OnNavigateToPage(NavigateToPageMessage message) 
    { 
     Type type = Type.GetType("MvvmLightFrame.Views." + message.Page.Substring(1), false); 
     if (type == null) 
     { 
      if (Debugger.IsAttached) 
       Debugger.Break(); 
      return; 
     } 

     var frame = Application.Current.MainWindow.GetVisualDescendents().OfType<Frame>().FirstOrDefault(); 
     if (frame == null) 
      return; 


     if (typeof(Layout.PageBase).IsAssignableFrom(type)) 
     { 
      var page = (Layout.PageBase)Activator.CreateInstance(type); 
      page.NavigationContext.Parameters = message.Parameters; 
      frame.Navigate(page); 
     } 
     else if (typeof(PageBase).IsAssignableFrom(type)) 
     { 
      var page = (PageBase)Activator.CreateInstance(type); 
      page.NavigationContext.Parameters = message.Parameters; 
      frame.Navigate(page); 
     } 
     else if (typeof(Page).IsAssignableFrom(type)) 
     { 
      frame.Navigate(Activator.CreateInstance(type)); 
     } 

     UpdateCanGoBack(); 
    } 

고마워.

소스 코드 : https://1drv.ms/u/s!AsQm4isQSZWotXKNEKgtcj0EVMjM

enter image description here

답변

1

PrismEventAggregator으로, 당신은 Subscribe 대신 RegisterPublish 대신 Send 사용합니다.

은 다음과 같습니다

public class MyEvent : PubSubEvent<string> { } 

public class MyViewModel 
{ 
private readonly IEventAggregator _eventAggregator = new EventAggregator(); 

public MyViewModel() 
{ 
    _eventAggregator.GetEvent<MyEvent>().Subscribe(GetMessage); 
} 

private void GetMessage(string text) 
{ 
    MessageBox.Show(text); 
} 

private DelegateCommand _pressButtonCommand; 
public DelegateCommand PressButtonCommand 
{ 
    get 
    { 
     return _pressButtonCommand ?? (_pressButtonCommand = new DelegateCommand(PressButton)); 
    } 
} 

private void PressButton() 
{ 
    _eventAggregator.GetEvent<MyEvent>().Publish("Hello World"); 
} 
}