出處:http://msdn.microsoft.com/en-us/magazine/jj694937.aspx
1. Define IDialogService:
public interface IDialogService { void ShowError(string errorMessage, string title, string buttonText); void ShowError(Exception error, string title, string buttonText); void ShowMessage(string message, string title, string buttonText); }
2. View implements IDialogService, register/unregister the Service by activate/inactivate the view:
public sealed partial class MainPage : IDialogService ... protected override void OnNavigatedTo(NavigationEventArgs e) { Messenger.Default.Register<StatusMessage>( this, HandleStatusMessage); SimpleIoc.Default.Register<IDialogService>(() => this); base.OnNavigatedTo(e); } protected override void OnNavigatedFrom(NavigationEventArgs e) { Messenger.Default.Unregister<StatusMessage>( this, HandleStatusMessage); SimpleIoc.Default.Unregister<IDialogService>(); base.OnNavigatedFrom(e); } // IDialogService implementation ... Popup a Messagebox ect.
3. Get IDialogService in ViewModel from Ioc Container:
public IDialogService DialogService { get { return ServiceLocator.Current.GetInstance<IDialogService>(); } }
4. using the DialogService in ViewModel:
... if (list.Count == 0) { DialogService.ShowMessage( "We couldn't find any articles", "Nothing found", "Too bad!"); } ...