Fork me on Github

Prism_对话框

一.注册对话框

/// <summary>
/// 注入服务
/// </summary>
/// <param name="containerRegistry"></param>
/// <exception cref="NotImplementedException"></exception>
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterDialog<UCDialog, UCDialogViewModel>("dialog");
}

二.对话框ViewModel必须实现 IDialogAware 接口

public class UCDialogViewModel : IDialogAware
{

    public string Title { get; set; } = "温馨提示";

    DialogCloseListener IDialogAware.RequestClose => throw new NotImplementedException();

    public DialogCloseListener RequestClose;

    /// <summary>
    /// 是否可以关闭对话框
    /// </summary>
    /// <returns></returns>
    public bool CanCloseDialog()
    {
        return true;
    }

    /// <summary>
    /// 关闭
    /// </summary>
    public void OnDialogClosed()
    {
        
    }

    /// <summary>
    /// 打开对话框
    /// </summary>
    /// <param name="parameters"></param>
    public void OnDialogOpened(IDialogParameters parameters)
    {

    }

}

三.传参

1.传参到对话框

 /// <summary>
 /// 显示对话框
 /// </summary>
 public void ShowDialog(string dialogName)
 {
     DialogParameters keyValuePairs = new DialogParameters();
     keyValuePairs.Add("Content", "这是传递的内容!");
     DialogService.ShowDialog(dialogName);
 }

2.对话框接收参数

/// <summary>
/// 打开对话框
/// </summary>
/// <param name="parameters"></param>
public void OnDialogOpened(IDialogParameters parameters)
{
    if (parameters.ContainsKey("Content"))
        Content = parameters.GetValue<string>("Content");
}

3.对话框回传参数

public class UCDialogViewModel : BindableBase, IDialogAware
{
    public UCDialogViewModel()
    {
        Content = "UCDialog";
        ConfirmComm = new DelegateCommand(Confirm);
        CancelComm = new DelegateCommand(Cancel);
    }

    public string Title { get; set; } = "温馨提示";
    private string content;

    public DelegateCommand ConfirmComm { get; set; }
    public DelegateCommand CancelComm { get; set; }

    public string Content
    {
        get { return content; }
        set
        {
            content = value;
            RaisePropertyChanged();
        }
    }

    DialogCloseListener IDialogAware.RequestClose => throw new NotImplementedException();

    public DialogCloseListener RequestClose;

    /// <summary>
    /// 是否可以关闭对话框
    /// </summary>
    /// <returns></returns>
    public bool CanCloseDialog()
    {
        return true;
    }

    /// <summary>
    /// 关闭
    /// </summary>
    public void OnDialogClosed() { }

    /// <summary>
    /// 打开对话框
    /// </summary>
    /// <param name="parameters"></param>
    public void OnDialogOpened(IDialogParameters parameters)
    {
        if (parameters.ContainsKey("Content"))
            Content = parameters.GetValue<string>("Content");
    }

    void Confirm()
    {
        DialogParameters dialogParameters = new DialogParameters();
        dialogParameters.Add("Content", Content);
        RequestClose.Invoke(dialogParameters, ButtonResult.Yes);
    }

    void Cancel()
    {
        RequestClose.Invoke(ButtonResult.No);
    }
}

4.回调函数接收对话框回传参数

/// <summary>
/// 显示对话框
/// </summary>
public void ShowDialog(string dialogName)
{
    DialogParameters keyValuePairs = new DialogParameters();
    keyValuePairs.Add("Content", "这是传递的内容!");
    string res;
    DialogService.ShowDialog(
        dialogName,
        keyValuePairs,
        callback =>
        {
            if (callback.Result == ButtonResult.Yes)
            {
                if (callback.Parameters.ContainsKey("Content"))
                    res = callback.Parameters.GetValue<string>("Content");
            }
        }
    );
}

 

posted @ 2025-05-13 23:08  昂昂呀  阅读(187)  评论(0)    收藏  举报