为什么显示我的 ContentDialog 的异步/等待代码在运行时失败? [英] Why does the async/await code to show my ContentDialog fail at runtime?

查看:21
本文介绍了为什么显示我的 ContentDialog 的异步/等待代码在运行时失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 UWP 应用中实例化一个 ContentDialog,它在我的 XAML 中定义如下:

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:

所以我把代码改成这样,添加了async"到方法/事件处理程序和等待"调用以显示内容对话框:

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();
}

因此,应用程序编译并运行,但是当我选择LoadMaps"时按钮,我明白了:

As such, the app compiles and runs, but when I select the "LoadMaps" button, I get this:

然后,按 F5 继续后,我得到:

Then, after hitting F5 to continue, I get:

我的代码或 XAML 有什么问题或遗漏了什么?

What is wrong with or missing from my code or XAML?

根据 Roy Li 的要求,这里是加载按钮的 XAML:

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" />
    

推荐答案

我能够使用以下代码使其工作(几乎/或多或少).

I am able to get it to work (pretty much/more-or-less) using the following code.

这是相关的 XAML:

Here is the pertinent 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();
    }
}

这是我点击 btnCre8NewMap 时看到的:

This is what I see when I click btnCre8NewMap:

这篇关于为什么显示我的 ContentDialog 的异步/等待代码在运行时失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆