MVVM Light Toolkit DialogService Sample


出處: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!");
                }
...

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM