(二十一)自定义询问窗口

需求

  • 关闭系统时提示
  • 删除待办时提示
  • 删除备忘录时提示

1.提示页面编辑

md命名空间引入

<Grid.RowDefinitions>
    <RowDefinition Height="auto" />
    <RowDefinition />
    <RowDefinition Height="auto" />
</Grid.RowDefinitions>

<TextBlock
    Padding="5"
    d:Text="温馨提示"
    FontSize="14"
    Text="{Binding Title}" />
<TextBlock
    Grid.Row="1"
    Padding="15,0"
    VerticalAlignment="Center"
    d:Text="确认删除该数据吗"
    FontSize="14"
    Text="{Binding Content}" />

<StackPanel
    Grid.Row="2"
    Margin="10"
    HorizontalAlignment="Right"
    Orientation="Horizontal">
    <Button
        Margin="0,0,10,0"
        Command="{Binding CancelCommand}"
        Content="取消"
        Style="{StaticResource MaterialDesignOutlinedButton}" />
    <Button Command="{Binding SaveCommand}" Content="确定" />
</StackPanel>

2.对于的viewmodel

AddToDoViewModel的拿来改

 //消息窗口一般也会传递数据
 public class MsgViewModel:BindableBase,IDialogHostAware
 {
     public MsgViewModel()
     {
         SaveCommand = new DelegateCommand(Save);
         CancelCommand = new DelegateCommand(Cancel);
     }
     private string title;
     public string Title
     {
         get { return title; }
         set { title = value;RaisePropertyChanged(); }
     }

     private string content;
     public string Content
     {
         get { return content; }
         set { content = value; RaisePropertyChanged(); }
     }
     private void Cancel()
     {
         if (DialogHost.IsDialogOpen(DialogHostName))
             DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No));
     }
     private void Save()
     {
         if (DialogHost.IsDialogOpen(DialogHostName))
         {
             DialogParameters param = new DialogParameters();
             DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.OK, param));
         }

     }
     public string DialogHostName { get; set; } = "Root";
     public DelegateCommand SaveCommand { get; set; }
     public DelegateCommand CancelCommand { get; set; }

     public void OnDialogOpend(IDialogParameters parameters)
     {
         if(parameters.ContainsKey("Title"))
         Title = parameters.GetValue<string>("Title");//要保证parameters里面有Title

         if(parameters.ContainsKey("Content"))
         Content = parameters.GetValue<string>("Content");
     }
 }

3.注册

image

以上完成了提示框的基本框架

4. 添加对话框的扩展方法

/// <summary>
/// 公共询问窗口
/// </summary>
/// <param name="dialogHost">指定的dialoghost会话主机</param>
/// <param name="title">标题</param>
/// <param name="content">询问内容</param>
/// <param name="dialogHostName">会话主机名称(唯一)</param>
/// <returns></returns>
public static async Task<IDialogResult> Question(this IDialogHostService dialogHost,
    string title, string content, string dialogHostName = "Root")
{
    DialogParameters param = new DialogParameters();
    param.Add("title", title);
    param.Add("content", content);
    param.Add("dialogHostName", dialogHostName);

    var dialogResult = await dialogHost.ShowDialog("MsgView", param, dialogHostName);
    return dialogResult;
}

5. 使用

在todoviewmodel和memoviewmodel中
image
然后再delete方法中添加

 var dialogResult = await dialogHost.Question("温馨提示",$"确认删除待办事项:{obj.Title} ?");
 if (dialogResult.Result != Prism.Services.Dialogs.ButtonResult.OK) return;

修改一下
image

6。 系统关闭的提示

再main view.cs中在关闭窗口的回调方法中

var dialogResult= await dialogHostService.Question("温馨提示", "确认退出系统?");
if (dialogResult.Result != Prism.Services.Dialogs.ButtonResult.OK) return;
posted @ 2023-10-12 15:16  huihui不会写代码  阅读(38)  评论(0)    收藏  举报