[英]when to use await key word? do I need it in this case?
i have a onClick method on a buttom.我在按钮上有一个 onClick 方法。 when this button is pressed, i get a displayalert
message.当按下此按钮时,我收到一条displayalert
消息。
getTokenQ has a value. getTokenQ 有一个值。 my question why its going inside catch
block?我的问题为什么它会进入catch
块?
private async void Button_Clicked(object sender, EventArgs e)
{
try
{
var getTokenQ = await SecureStorage.GetAsync("Save_Security_Question");
if ((String.IsNullOrWhiteSpace(getTokenQ) == false))
{
}
else
{
}
}
catch (Exception ex)
{
await DisplayAlert("Message", "Some thing went wrong. Please try different method", "ok");
}
}
no you don't need to do that, you can use .Result if you don't want to execute your code asynchronously , this code is perfectly fine :不,您不需要这样做,如果您不想异步执行代码,可以使用 .Result ,此代码非常好:
private void Button_Clicked(object sender, EventArgs e)
{
try
{
var getTokenQ = SecureStorage.GetAsync("Save_Security_Question").Result;
if ((String.IsNullOrWhiteSpace(getTokenQ) == false))
{
}
else
{
}
}
catch (Exception ex)
{
DisplayAlert("Message", "Some thing went wrong. Please try different method", "ok");
}
}
Why in catch !为什么在抓! Because it's a message to display only if an exception is thrown.因为它是只有在抛出异常时才显示的消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.