首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >确认或取消MessageDialog

确认或取消MessageDialog
EN

Stack Overflow用户
提问于 2021-06-04 18:34:06
回答 2查看 70关注 0票数 1

我正在尝试创建一个函数,该函数显示一个对话框,用户应该在其中选择是确认删除表上的条目还是取消它。

我已经搜索了这个主题,这就是我在代码方面所达到的:

代码语言:javascript
复制
public static bool ConfirmDelete(string table, string rowid)
{


   var result = PopupMessageHelper.ShowWithOptionsAsync($"Tem a certeza que pretende eliminar a linha correspondente ao identificador {rowid} da tabela {table}").Result;

        if (!result)
        {
            return false;
        } else if (result)
        {
            return true;
        }
        return false;
    }


 public static async Task<bool> ShowWithOptionsAsync(string text)
        {
            var yesCommand = new UICommand(LocalizationHelper.GetValue("txt_keyword_submit"));
            var noCommand = new UICommand(LocalizationHelper.GetValue("txt_keyword_cancel"));

            var messageDialog = new MessageDialog(text);
            messageDialog.Options = MessageDialogOptions.None;
            messageDialog.Commands.Add(yesCommand);
            messageDialog.Commands.Add(noCommand);
          
            var command = await messageDialog.ShowAsync();

            if (command == yesCommand)
            {
                return true;
            }
            else if (command == noCommand)
            {
                return false;
            }
            else
            {
                return false;
            }  
        }
    }

Visual studio不会返回任何错误,但当我执行该部分代码时,它会死锁。关于我可以让它工作的任何提示吗?:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-04 21:20:37

您的ConfirmDelete方法应返回Task<bool>

代码语言:javascript
复制
public static async Task<bool> ConfirmDelete(string table, string rowid)
{
    var result = await PopupMessageHelper
        .ShowWithOptionsAsync($"Tem a certeza que pretende eliminar a linha correspondente ao identificador {rowid} da tabela {table}");

    if (!result)
    {
        return false;
    }
    else if (result)
    {
        return true;
    }
    return false;
}

...and在您调用它的方法中等待:

代码语言:javascript
复制
private async void Button_Click(object sender, RoutedEventArgs e)
{
    await ConfirmDelete("...", "...");
}

不应使用.Result属性进行阻止。

票数 1
EN

Stack Overflow用户

发布于 2021-06-04 18:42:35

由于您正在使用异步方法,因此您可能需要等待一段时间

像这样:

代码语言:javascript
复制
public static async Task<bool> ConfirmDeleteAsync(string table, string rowid)
{
    return await PopupMessageHelper.ShowWithOptionsAsync($"Tem a certeza que pretende eliminar a linha correspondente ao identificador {rowid} da tabela {table}");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67835762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档