简体   繁体   English

UWP阻止MessageDialog关闭父ContentDialog

[英]UWP Preventing MessageDialog from Closing Parent ContentDialog

My app uses ContentDialog as a mean for data insertion. 我的应用程序使用ContentDialog作为插入数据的方式。 In other words; 换一种说法; the data form is a ContentDialog. 数据形式是ContentDialog。 During validating the user input, the app shall prompt any error to the user by using MessageDialog. 在验证用户输入期间,应用程序应使用MessageDialog向用户提示任何错误。 However, dismissing the MessageDialog will also dismissing the ContentDialog. 但是,关闭MessageDialog也将关闭ContentDialog。

Here is the chunk of the code when the alert is shown: 这是显示警报时的代码块:

private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
    //save item
    ValidateForm();
}

private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
}

private async void ValidateForm()
{
    //Ensure all fields are filled
    String barcode = BarcodeText.Text.Trim();
    String desc = DescText.Text.Trim();
    String cost = CostText.Text.Trim();
    String price = PriceText.Text.Trim();
    String stock = StockText.Text.Trim();

    if(barcode.Equals(String.Empty) || desc.Equals(String.Empty) ||
            desc.Equals(String.Empty) || cost.Equals(String.Empty) || 
            price.Equals(String.Empty) || stock.Equals(String.Empty))
    {
        var dialog = new MessageDialog("Please fill in all fields");
        await dialog.ShowAsync();
        return;
    }
    //check uniqueness of the barcode

}

What should I do to prevent the alert from closing the parent ContentDialog? 我应如何防止警报关闭父级ContentDialog?

The ContentDialog is automatically dismissed when PrimaryButton or SecondaryButton are clicked. 单击PrimaryButton或SecondaryButton时,将自动关闭ContentDialog。 To override this behaviour you must set the args.Cancel property to true . 要覆盖此行为,必须将args.Cancel属性设置为true And since ValidateForm is async method, you also need to take a deferral as Raymond Chen said. 而且由于ValidateForm是一种异步方法,因此您还需要按照Raymond Chen所说进行延期。

So if you don't want to close the ContentDialog when the MessageDialog had been shown, the code will look somehow like this: 因此,如果您不希望在显示MessageDialog时关闭ContentDialog,则代码将如下所示:

private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
    //save item
    var deferral = args.GetDeferral()
    args.Cancel = await ValidateForm();
    deferral.Complete();
}

private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
}

// Returns true if the MessageDialog was shown, otherwise false
private async Task<bool> ValidateForm()
{
    //Ensure all fields are filled
    String barcode = BarcodeText.Text.Trim();
    String desc = DescText.Text.Trim();
    String cost = CostText.Text.Trim();
    String price = PriceText.Text.Trim();
    String stock = StockText.Text.Trim();

    if(barcode.Equals(String.Empty) || desc.Equals(String.Empty) ||
            desc.Equals(String.Empty) || cost.Equals(String.Empty) || 
            price.Equals(String.Empty) || stock.Equals(String.Empty))
    {
        var dialog = new MessageDialog("Please fill in all fields");
        await dialog.ShowAsync();
        return true;
    }
    //check uniqueness of the barcode
    return false;
}

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

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