簡體   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