繁体   English   中英

我如何检查是否在 WinUI3 的 ContentDialog 中单击了 CloseButton

[英]How can i check if the CloseButton is clicked in a ContentDialog in WinUI3

我想检查是否在 ContentDialog 中单击了 CloseButton。

var inputTextBox = new TextBox
{
    AcceptsReturn = true,
    Height = 32,
    Width = 300,
    Text = string.Empty,
    TextWrapping = TextWrapping.Wrap,
};

if (Controller.CheckVersion() == 1)
{
    ContentDialog dialog = new ContentDialog
    {
        XamlRoot = this.XamlRoot,
        Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
        Title = "Please write something!",
        Content = inputTextBox,
        CloseButtonText = "Select",
    };

#pragma warning restore IDE0090

    _ = await dialog.ShowAsync();
}

WinUI3 中的ContentDialog是否有类似IsClicked()的方法? 所以我可以检查用户是否在 TextBox 中写了一些东西。

MS 文档中包含一个非常简单的示例:

private async void ShowTermsOfUseContentDialogButton_Click(object sender, RoutedEventArgs e)
{
    ContentDialogResult result = await termsOfUseContentDialog.ShowAsync();
    if (result == ContentDialogResult.Primary)
    {
        // Terms of use were accepted.
    }
    else
    {
        // User pressed Cancel, ESC, or the back arrow.
        // Terms of use were not accepted.
    }
}

private void TermsOfUseContentDialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args)
{
    // Ensure that the check box is unchecked each time the dialog opens.
    ConfirmAgeCheckBox.IsChecked = false;
}

private void ConfirmAgeCheckBox_Checked(object sender, RoutedEventArgs e)
{
    termsOfUseContentDialog.IsPrimaryButtonEnabled = true;
}

private void ConfirmAgeCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    termsOfUseContentDialog.IsPrimaryButtonEnabled = false;
}

内容对话框类

您可以将PrimaryButton用于Select并使用CloseButton用于Cancel 您还可以根据文本输入启用/禁用选择按钮。

var inputTextBox = new TextBox
{
    AcceptsReturn = true,
    Height = 32,
    Width = 300,
    Text = string.Empty,
    TextWrapping = TextWrapping.Wrap,
};

if (Controller.CheckVersion() == 1)
{
    ContentDialog dialog = new ContentDialog
    {
        XamlRoot = this.XamlRoot,
        Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
        Title = "Please write something!",
        Content = inputTextBox,
        IsPrimaryButtonEnabled  = false,
        PrimaryButtonText = "Select",
        CloseButtonText = "Cancel",
    };

    inputTextBox.TextChanged += (s, e) =>
    {
        dialog.IsPrimaryButtonEnabled = (s as TextBox)?.Text.Count() > 0;
    };

#pragma warning restore IDE0090

    // Is the "Select" button clicked?
    if (await dialog.ShowAsync() is ContentDialogResult.Primary)
    {
        var text = inputTextBox.Text;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM