[英]Why does the async/await code to show my ContentDialog fail at runtime?
I want to instantiate a ContentDialog in my UWP app, which is defined in my XAML as follows:我想在我的 UWP 应用程序中实例化一个 ContentDialog,该应用程序在我的 XAML 中定义如下:
<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:所以我把代码改成这个,在方法/事件处理程序中添加“async”,在调用中添加“await”以显示内容对话框:
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:因此,应用程序编译并运行,但是当我 select “LoadMaps”按钮时,我得到了这个:
Then, after hitting F5 to continue, I get:然后,在按 F5 继续后,我得到:
What is wrong with or missing from my code or XAML?我的代码或 XAML 有什么问题或缺少什么?
Per Roy Li's request, here is the XAML for the load button:根据 Roy Li 的要求,这里是加载按钮的 XAML:
<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.在堆栈上而不是在页面 Xaml 上创建您的 ContentDialog。 You can create a new ContentDialog class or create a UserControl class with the xaml.
您可以创建一个新的 ContentDialog class 或使用 xaml 创建一个 UserControl class。 See the sample here .
请参阅此处的示例。
private async void btnLoadMap_Click(object sender,
RoutedEventArgs e)
{
var content = new MyUserControl();
var dialog = new ContentDialog
{
Content = content
};
await dialog.ShowAsync();
}
I am able to get it to work (pretty much/more-or-less) using the following code.我可以使用以下代码使其工作(几乎/或多或少)。
Here is the pertinent XAML:这是相关的 XAML:
<Button x:Name="btnCre8NewMap" Content="Create New Map" ToolTipService.ToolTip="Create a new map" Margin="140,16,50,0" VerticalAlignment="Top" Click="btnCre8NewMap_Click"/>
. . .
<ContentDialog x:Name="cntDlgCre8Map"
Title="Create a New Map"
PrimaryButtonText="Save"
CloseButtonText="Cancel"
DefaultButton="Primary">
<StackPanel>
<TextBlock Text="Map Name: "/>
<TextBox x:Name="txtbxMapName"
Width="300" HorizontalAlignment="Left"/>
<TextBlock Text="Default Zoom Level: "/>
<ComboBox x:Name="cmbxCre8MapZoomLevels"
Width="100" HorizontalAlignment="Left"/>
<TextBlock Text="Map Notes: "/>
<TextBox x:Name="txtbxMapNotes"
Width="300" Height="300" HorizontalAlignment="Left"/>
</StackPanel>
</ContentDialog>
...and here is the button click event in the code-behind: ...这是代码隐藏中的按钮单击事件:
private async void btnCre8NewMap_Click(object sender, RoutedEventArgs e)
{
try
{
string mapName = string.Empty;
string mapNotes = string.Empty;
int defaultZoomLevel = 1;
ClearLocations();
// Popul8 the cmbx
for (int i = 1; i < 20; i++)
{
cmbxCre8MapZoomLevels.Items.Add(i.ToString());
}
ContentDialogResult result = await cntDlgCre8Map.ShowAsync();
if (result == ContentDialogResult.Primary)
{
mapName = txtbxMapName.Text;
mapNotes = txtbxMapNotes.Text;
defaultZoomLevel = cmbxCre8MapZoomLevels.SelectedIndex + 1;
InsertMapRecord(mapName, mapNotes, preferredZoomLevel);
}
// else do nothing (don't save)
}
catch (Exception ex)
{
MessageDialog exceptionMsgDlg = new MessageDialog(ex.Message, "btnCre8NewMap_Click");
await exceptionMsgDlg.ShowAsync();
}
}
This is what I see when I click btnCre8NewMap:这是我单击 btnCre8NewMap 时看到的内容:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.