¿Cuál es la forma correcta de usar la clase Messenger
? Sé que se puede usar para comunicaciones ViewModels/Views, pero ¿es una buena forma de utilizarlo para una capa de servicio técnico/comercial?Forma correcta de usar MVVM Light Messenger
Por ejemplo, un servicio de registro/navegación registra algunos mensajes en los constructores y sabe cuándo se producen estos mensajes en la aplicación. El remitente (ViewModel ou Service) no hace referencia a la interfaz de servicio, sino solo a Messenger para enviar mensajes. Este es un servicio de muestra:
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;
}
}
}
Para obtener más información: [Eventos globales considerados nocivos] (http://dkturner.blogspot.hu/2010/06/global-events-considered-harmful.html) – nemesv
Gracias, parece ser un enfoque goof. pero no es específico de MVVM Messenger – Cybermaxs