繁体   English   中英

结果来自 Await DisplayAlert Xamarin.Forms

[英]result from Await DisplayAlert Xamarin.Forms

Xamarin具有可等待的DisplayAlert方法,该方法返回Task<bool>

我有staticHelpers ,我想在其中编写这样的代码:

public static bool DisplayAlert(string title, string message, string accept, string cancel){
    //return answer from Page.DisplayAlert(title, message, accept, cancel);
}

此方法将从应用程序的其他部分调用,例如:

public ... SomeMethod(){
    // some code
    bool result = DisplayAlert(...);
    // some code
}

我想在SomeMethod避免async/await (如何异步等待用户的响应?!!!!!!)

我试过await Page.DisplayAlert(...)task.Wait()和另一个。 但是Task是异步执行的,没有返回值。 或者应用程序永远冻结而不显示DisplayAlert

怎么做? 最佳做法是什么?

如果您希望SomeMethod保持同步,这是一种选择:

public async static Task<bool> DisplayAlert(string title, string message, string accept, 
string cancel){
 return await Page.DisplayAlert(title, message, accept, cancel);
}

// SomeMethod will run Synchronously
public boolean SomeMethod(){
  bool result = StaticClass.DisplayAlert(..).Result;
  return result;
}

编辑:修正了一些语法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM