[Prism] Dialog service

1. Main functions

        The main function provided by prism's Dialog service is to improve the original Dialog service, providing the function of parameter passing and callback function. The function of this framework is only used when Dialog (pop-up window) is needed. (Remember, this is only used when a popup is required).

2. Construction process

        Based on Prism's MVVM binding mode, construct the corresponding view and viewmodel respectively. For example MesView and MesViewModel. Then register in the RegisterTypes method in the App class. Build a dependency injection relationship.       

containerRegistry.RegisterDialog<MesView, MesViewModel>();

Then bind the DataContext of MVVM, and MesViewModel needs to implement the interface IDialogAware to use the Dialog service

    internal class MesViewModel : BindableBase, IDialogAware
    {
        public string Title => throw new NotImplementedException();

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            throw new NotImplementedException();
        }

        public void OnDialogClosed()
        {
            throw new NotImplementedException();
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            throw new NotImplementedException();
        }
    }

These are the methods and properties that need to be implemented in the interface.

        OnDialogOpened is a function executed when the window is opened, and parameters can be passed, and the parameters are in the form of DialogParameter.

        OnDialogClosed function executed when the window is closed,

        CanCloseDialog returns a bool value, return true;

        The RequestClose delegate is called to exit the Dialog. Parameters can be passed at execution time. No matter what parameters are passed, this delegate will exit the Dialog.

                DialogParameters temp = new DialogParameters();
                temp.Add("Key2", Title);
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK,temp));

                RequestClose?.Invoke(new DialogResult(ButtonResult.No, null));

This shows the usage of passing parameters when exiting. Or pass parameters in the form of key-value pairs.

3. Call Dialog

        In the class that calls Dialog, add the property:

        public IDialogService dialogService { get; set; }

Then initialize in the constructor of this class, where the constructor adds virtual parameters of IDialogService dialogServiceTemp without providing corresponding actual parameters.

        Call the ShowDialog method in the corresponding DelegatCommmand that opens the Dialog:

                dialogService.ShowDialog("MesView",para, arg =>
                {
                    if(arg.Result==ButtonResult.OK)
                    {
                        string str = arg.Parameters.GetValue<string>("Key2");
                        MessageBox.Show(str);
                    }
                    else
                    {
                        MessageBox.Show("Cancel");
                    }
                }); 

This method can pass a parameter to the inside of the dialog, and at the same time construct a callback method, which will be executed when the Dialog exits, that is, the last execution.

        arg is a variable of IDialogResult type, which is the virtual parameter passed in when RequestClose is executed.

Four, Dialog Result delivery

        Dialog.Result is passed when calling RequestClose?.Invoke. If ButtonResult.OK is selected, then this DialogResult will also be ButtonResult.OK.

5. Passing in and out of parameters

        First, the parameters are passed into the Dialog from the outside, and are passed in in the method dialogService.ShowDialog("MesView", para, arg =>()). Parameters are transferred in the type DialogParameters

        If the parameter is passed out, call this function in RequestClose?.Invoke(new DialogResult(ButtonResult.OK, temp)) to add a DialogResult parameter, which is the arg parameter obtained by the ShowDialog method. ,

string str = arg.Parameters.GetValue<string>("Key2");

This value can be retrieved as a key-value pair.

Guess you like

Origin blog.csdn.net/weixin_43163656/article/details/127900295