我正在尝试创建一个函数,该函数显示一个对话框,用户应该在其中选择是确认删除表上的条目还是取消它。
我已经搜索了这个主题,这就是我在代码方面所达到的:
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不会返回任何错误,但当我执行该部分代码时,它会死锁。关于我可以让它工作的任何提示吗?:)
发布于 2021-06-04 21:20:37
您的ConfirmDelete方法应返回Task<bool>
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在您调用它的方法中等待:
private async void Button_Click(object sender, RoutedEventArgs e)
{
await ConfirmDelete("...", "...");
}不应使用.Result属性进行阻止。
发布于 2021-06-04 18:42:35
由于您正在使用异步方法,因此您可能需要等待一段时间
像这样:
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}");
}https://stackoverflow.com/questions/67835762
复制相似问题