首页 > 解决方案 > 确认或取消消息对话框

问题描述

我正在尝试制作一个显示对话框的函数,用户应在该对话框中选择确认删除表中的条目或取消它。

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

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 没有返回任何错误,但是当我执行那部分代码时,它会死锁。关于我可以得到这个工作的任何提示?:)

标签: c#.netuwp

解决方案


当您使用异步方法时,可能您需要某种 await 那里

像:

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

推荐阅读