我不明白为什么主线程不交出控制权来处理结果。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; } }我调用方法 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;请帮忙!我不明白僵局发生在哪里
1 回答
手掌心
TA贡献1942条经验 获得超3个赞
您的async void
非事件处理程序意味着您的即发即忘调用将无法捕获可能引发的任何异常。
使用事件处理程序修复该问题
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 关注
- 111 浏览
添加回答
举报
0/150
提交
取消