0
我需要一些建議。有誰知道如何將MVVM Light的Messenger轉換爲Prims。WPF MVVM Light Messenger和Prism EventAggregator
我有這個代碼從MVVM光轉換上棱鏡的一個問題:
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光的全部代碼:
/// <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