I want to instantiate a ContentDialog in my UWP app, which is defined in my XAML as follows:
<Page
. . .
<Grid x:Name="grd">
. . .
<ContentDialog x:Name="cntDlgLoadMap"
Title="This is an example"
PrimaryButtonText="Ok"
CloseButtonText="Cancel"
DefaultButton="Primary">
</ContentDialog>
</Grid>
</Page>
Trying to get a minimal example running, I was going to try this:
private void btnLoadMap_Click(object sender, RoutedEventArgs e)
{
cntDlgLoadMap.ShowAsync();
}
...but got this design-time err msg:
So I changed the code to this, adding "async" to the method/event handler and "await" to the call to show the content dialog:
private async void btnLoadMap_Click(object sender, RoutedEventArgs e)
{
await cntDlgLoadMap.ShowAsync();
}
As such, the app compiles and runs, but when I select the "LoadMaps" button, I get this:
Then, after hitting F5 to continue, I get:
What is wrong with or missing from my code or XAML?
UPDATE
Per Roy Li's request, here is the XAML for the load button:
<Button x:Name="btnLoadMap" Content="Load Map" Margin="20,16,50,0" VerticalAlignment="Top" Click="btnLoadMap_Click" />
Create your ContentDialog on the stack not on the page Xaml. You can create a new ContentDialog class or create a UserControl class with the xaml. See the sample here.