簡體   English   中英

WPF MVVM Light Messenger和Prism EventAggregator

[英]WPF MVVM Light Messenger and Prism EventAggregator

我需要一些建議。 有誰知道如何將MVVM Light的Messenger轉換為Prims。

我在從Prism的MVVM Light轉換此代碼時遇到問題:

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

非常感謝。

源代碼: https : //1drv.ms/u/s!AsQm4isQSZWotXKNEKgtcj0EVMjM

在此處輸入圖片說明

使用Prism EventAggregator ,可以使用“ Subscribe代替“ Register ,並使用“ Publish代替“ 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");
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM