2016-09-26 5 views
0

Ich brauche einen Ratschlag. Kann jemand Messenger von MVVM Light zu Prims konvertieren?WPF MVVM Light Messenger und Prism EventAggregator

Ich habe ein Problem mit diesem Code aus dem MVVM Light on Prism zu konvertieren:

MVVM Licht

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

Prism

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

der gesamte Code von MVVM Light:

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

Vielen Dank.

Quellcode: https://1drv.ms/u/s!AsQm4isQSZWotXKNEKgtcj0EVMjM

enter image description here

Antwort

1

Mit PrismEventAggregator verwenden Sie Subscribe statt Register und Publish statt Send.

Es sieht wie folgt aus:

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