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