async await execution windows phone 8

无人久伴 提交于 2019-12-08 13:58:26

You should avoid async void. I explain this guideline in an MSDN article.

Once you change your async void method to async Task:

public async Task GetCategoriesAsync()
{            
  Categorieslist = new ObservableCollection<Categories>(await PhoneClient.GetDefaultCategories());          
}

Then you can await it as such:

protected override async void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
  await categoriesvm.GetCategoriesAsync();                 
  BookCategories.DataContext = from vm in categoriesvm.Categorieslist select vm;
}

However, I recommend doing all your VM initialization outside of your UI events - this will make your code easier to test. Take a look at my async constructors blog post for ideas.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!