ContentDialog.ShowAsync()的结果总是None

本文关键字:None 结果 ShowAsync ContentDialog | 更新日期: 2023-09-27 18:13:21

我有一个简单的ContentDialog与ContentDialog。XAML:

<ContentDialog x:Class="SampleApp.Dialogs"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:local="using:SampleApp.Dialog"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           PrimaryButtonClick="OkClick"
           PrimaryButtonText="OK"
           SecondaryButtonText="Cancel"
           SecondaryButtonClick="CancelClick"
           mc:Ignorable="d">
<StackPanel>
    <TextBox 
             Text="{x:Bind NameOne, Mode=TwoWay}" />
    <TextBox  Text="{x:Bind NameTwo, Mode=TwoWay}" />
</StackPanel>

在代码后面:

     private void OkClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
    {
       this.Hide();
    }

当我想初始化这个对话框时:

Dialog dialog = new Dialog();
var result = await dialog.ShowAsync();

result总是ContentDialogResult.None类型。即使我点击"确定"或"取消",但当我点击"确定"时,它应该至少是ContentDialogResult.Primary。这是一种预期行为吗?

ContentDialog.ShowAsync()的结果总是None

问题是PrimaryButtonClick="OkClick", OkClick覆盖默认操作(这是隐藏),因此结果总是None。去掉这个问题就解决了。

private void OkClick(object sender, RoutedEventArgs e)
{
    this.Result = MyResult.Yes;
    // Close the dialog
    dialog.Hide();
}