我不明白為什么主線程不交出控制權來處理結(jié)果。public partial class NewTravelPage : ContentPage { public NewTravelPage() { InitializeComponent(); } protected async override void OnAppearing() { var locator = CrossGeolocator.Current; var position = await locator.GetPositionAsync(); var vanues = await VenueLogic.getVenues(position.Latitude, position.Longitude); venueListView.ItemsSource = vanues; } }我調(diào)用方法 getVenues:public class VenueLogic { public async static Task<List<Venue>> getVenues(double latitude, double longitude) { List<Venue> vanues = new List<Venue>(); var url = VenueRoot.GenerateUrl(latitude, longitude); using (HttpClient client = new HttpClient()) { var res = await client.GetAsync("https://stackoverflow.com"); // here the code gives control to the main thread and stucks var response = await res.Content.ReadAsStringAsync(); var venueRoot = JsonConvert.DeserializeObject<VenueRoot> (response); vanues = venueRoot.response.venues as List<Venue>; } return vanues; } }使用.NetStandard;請幫忙!我不明白僵局發(fā)生在哪里
1 回答

手掌心
TA貢獻1942條經(jīng)驗 獲得超3個贊
您的async void
非事件處理程序意味著您的即發(fā)即忘調(diào)用將無法捕獲可能引發(fā)的任何異常。
使用事件處理程序修復該問題
public partial class NewTravelPage : ContentPage {
? ? public NewTravelPage() {
? ? ? ? InitializeComponent();
? ? ? ? appearing += onAppearing;
? ? }
? ? protected override void OnAppearing() {
? ? ? ? appearing(this, EventArgs.Empty);
? ? }
? ? event EventHandler appearing = delegate { };
? ? private async void onAppearing(object sender, EventArgs args) {
? ? ? ? try {
? ? ? ? ? ? var locator = CrossGeolocator.Current;
? ? ? ? ? ? var position = await locator.GetPositionAsync();
- 1 回答
- 0 關注
- 148 瀏覽
添加回答
舉報
0/150
提交
取消