在一个方法中我有一个 try catch
像这样阻止:
try
{
// do something
}
catch (Exception ex)
{
MessageDialog message = new MessageDialog(ex.ToString());
message.ShowAsync();
}
我收到以下关于 message.ShowAsync()
行的警告:
Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
说完了:
try
{
// do something
}
catch (Exception ex)
{
MessageDialog message = new MessageDialog(ex.ToString());
await message.ShowAsync();
}
现在我得到一个异常(exception):
The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
我什至尝试这样做以避免在 catch block 内等待:
Exception exception;
try
{
// do something
}
catch (Exception ex)
{
exception = ex;
}
if (exception != null)
{
MessageDialog message = new MessageDialog(ex.ToString());
message.ShowAsync();
}
然而,这并没有改变任何东西。
我必须做什么才能使用 await
在这种情况下? MessageDialog.ShowAsync()
就 IntelliSense 显示返回 Windows.Foundation.IAsyncOperation<IUICommand>
的可等待方法而言.
最佳答案
您的函数必须是“异步的”才能使用异步运算符:
例如:
private async void Test()
{
MessageDialog message = new MessageDialog(ex.ToString());
await message.ShowAsync();
}
关于c# - 将 await 与 MessageDialog.ShowAsync() 一起使用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38050955/