【问题标题】:change MessageDialog content or show new one from MessageDialog handler Windows Store app更改 MessageDialog 内容或显示来自 MessageDialog 处理程序 Windows Store 应用程序的新内容
【发布时间】:2014-02-16 00:38:41
【问题描述】:

我有MessageDialog对话负责删除确认。

private async void ShowDialogClick(object sender, RoutedEventArgs e)
{
    MessageDialog md = new MessageDialog("Are your sure you want to delete this?");

    md.Commands.Add(new UICommand("Delete",
        new UICommandInvokedHandler(DeleteItemHandler)));
    md.Commands.Add(new UICommand("Cancel"));

    await md.ShowAsync();
}

当用户点击Delete时,DeleteItemHandler调用数据库操作,但是我如何通知用户操作不成功?

我尝试创建新的 MessageDialog,但得到了win32 exception

private async void DeleteItemHandler(IUICommand command)
{
    MessageDialog md = new MessageDialog("New content");

    String result = DbDeletation();

    if(result != "OK")
        await md.ShowAsync();
}

告知用户错误的最佳方式是什么?

【问题讨论】:

  • MessageDialog.ShowAsync() 的 MSDN 文章特别警告了这一点。一次只能激活一个对话框,当您的 DeleteItemHandler 运行时 MessageDialog 仍然处于活动状态。所以你不能在那个方法中显示另一个。考虑在对话框关闭后使用 Dispatcher.BeginInvoke() 稍后运行该代码。

标签: c# wpf xaml windows-8.1 win32exception


【解决方案1】:

不能自定义MessageDialos并连续调用,所以,有两种方式:

  1. 使用命令构建自己的弹出窗口控件,在操作返回结果之前不要关闭弹出窗口。显示进度或类似的东西。如果发生错误 - 在弹出窗口中直接显示。

  2. 使用 MessageDialog 并在调用 MessageDialog 的位置(例如,在按钮 Delete 附近)显示进度和错误消息(如果有)。

第二种方法更适合Windows Store App guidelines

【讨论】:

  • 感谢您的指导,我有很好的答案(MessageDialog 不是删除确认的好方法。
【解决方案2】:

根据Windows Store App guidelinesMessagegDialog不是确认删除的好方法。

当应用需要确认用户对用户已采取的操作的意图时,浮出控件是合适的表面。请参阅弹出指南。

现在我的代码更简洁了...

    private async void DeleteItem_Click(object sender, RoutedEventArgs e)
    {
        MessageDialog md = new MessageDialog("Error");

        String result = DbDeletation();

        if (result != "OK")
            await md.ShowAsync();
    }

更温和的解决方案:)

    <Button HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Content="Show Dialog">
        <Button.Flyout>
            <Flyout>
                <StackPanel>
                    <TextBlock>Are your sure you want to delte this?</TextBlock>
                    <Button Click="DeleteItem_Click"
                            Content="Delete"
                            HorizontalAlignment="Right"/>
                </StackPanel>
            </Flyout>
        </Button.Flyout>
    </Button>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多