Reputation: 3048
My app uses ContentDialog as a mean for data insertion. In other words; the data form is a ContentDialog. During validating the user input, the app shall prompt any error to the user by using MessageDialog. However, dismissing the MessageDialog will also dismissing the 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?
Upvotes: 1
Views: 1382
Reputation: 3492
The ContentDialog is automatically dismissed when PrimaryButton or SecondaryButton are clicked. To override this behaviour you must set the args.Cancel
property to true
. And since ValidateForm
is async method, you also need to take a deferral as Raymond Chen said.
So if you don't want to close the ContentDialog when the MessageDialog had been shown, the code will look somehow like this:
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;
}
Upvotes: 6