正确使用MVVM Light Messenger的方法
使用Messenger类的正确方法是什么? 我知道它可以用于ViewModels/Views通信,但在技术/业务服务层中使用它是一种好方法吗 例如,日志/导航服务在构造函数中注册一些消息,并在应用程序中出现这些消息时进行感知。发送方(ViewModel ou服务)不引用服务接口,而仅引用messenger来发送消息。以下是一个示例服务:正确使用MVVM Light Messenger的方法,mvvm,mvvm-light,messaging,Mvvm,Mvvm Light,Messaging,使用Messenger类的正确方法是什么? 我知道它可以用于ViewModels/Views通信,但在技术/业务服务层中使用它是一种好方法吗 例如,日志/导航服务在构造函数中注册一些消息,并在应用程序中出现这些消息时进行感知。发送方(ViewModel ou服务)不引用服务接口,而仅引用messenger来发送消息。以下是一个示例服务: using System; using System.Windows; using System.Windows.Navigation; using Micro
using System;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using App.Service.Interfaces;
using GalaSoft.MvvmLight.Messaging;
namespace App.Service
{
public class NavigationService : INavigationService
{
private PhoneApplicationFrame _mainFrame;
public event NavigatingCancelEventHandler Navigating;
public NavigationService()
{
Messenger.Default.Register<NotificationMessage<Uri>>(this, m => { this.NavigateTo(m.Content); });
}
public void NavigateTo(Uri pageUri)
{
if (EnsureMainFrame())
{
_mainFrame.Navigate(pageUri);
}
}
public void GoBack()
{
if (EnsureMainFrame()
&& _mainFrame.CanGoBack)
{
_mainFrame.GoBack();
}
}
private bool EnsureMainFrame()
{
if (_mainFrame != null)
{
return true;
}
_mainFrame = Application.Current.RootVisual as PhoneApplicationFrame;
if (_mainFrame != null)
{
// Could be null if the app runs inside a design tool
_mainFrame.Navigating += (s, e) =>
{
if (Navigating != null)
{
Navigating(s, e);
}
};
return true;
}
return false;
}
}
}
使用系统;
使用System.Windows;
使用System.Windows.Navigation;
使用Microsoft.Phone.Controls;
使用App.Service.Interfaces;
使用GalaSoft.MvvmLight.Messaging;
名称空间App.Service
{
公共类导航服务:INavigationService
{
专用电话应用框架\u主机;
公共事件导航CancelEventHandler导航;
公共导航服务()
{
Register(this,m=>{this.NavigateTo(m.Content);});
}
public void NavigateTo(Uri页面Uri)
{
if(inframe())
{
_大型机导航(pageUri);
}
}
公开作废GoBack()
{
if(inframe()
&&_大型机(CanGoBack)
{
_mainFrame.GoBack();
}
}
二等兵
{
如果(_mainFrame!=null)
{
返回true;
}
_mainFrame=Application.Current.RootVisual作为PhoneApplicationFrame;
如果(_mainFrame!=null)
{
//如果应用程序在设计工具内运行,则可能为null
_大型机。导航+=(s,e)=>
{
如果(导航!=null)
{
航行(s,e);
}
};
返回true;
}
返回false;
}
}
}
对我来说,messenger的主要用途是允许viewModels之间进行通信。假设您有一个viewmodel,用于为搜索功能提供业务逻辑,页面/窗口上有3个viewmodel希望处理搜索以显示输出,messenger将是以松散绑定的方式实现这一点的理想方式
获取搜索数据的viewmodel只需发送一条“搜索”消息,该消息将被当前注册以使用该消息的任何对象使用
这里的好处是:
查看上面的代码。想象一下,如果您必须为其中的每个方法(导航、CanNavigate、GoBack、GoForward等)编写一个事件。你会收到很多信息。您的代码也将更难理解。进一步阅读:谢谢,这似乎是一个愚蠢的方法。但这并不是MVVM信使特有的猜测这家伙窃取了你的答案?