异步执行的期待的Windows Phone 8 [英] async await execution windows phone 8
问题描述
我是新来的异步和等待的编程风格。我该如何解决以下问题:
我先调用低于code。这里的问题是,在第一线正在等待它需要填充 categoriesvm.Categorieslist
,它没有,但在第二行被调用。 (我认为是的await的默认行为)
我怎样才能确保第二行被称为只有当 categoriesvm.Categorieslist
在第一线填充?
保护覆盖无效的OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs E)
{
categoriesvm.GetCategories();
BookCategories.DataContext =从虚拟机中categoriesvm.Categorieslist选择虚拟机;
}
在code,当我执行它低于第一线之上,其中 Categorieslist
是我上面访问列表。
公共异步无效GetCategories()
{
Categorieslist =新的ObservableCollection<类别>(等待PhoneClient.GetDefaultCategories());
}
的 phoneclient
小于
公共类PhoneClient
{
私人静态只读HttpClient的客户端; 公共静态乌里ServerBaseUri
{
{返回新的URI(http://169.254.80.80:30134/api/); }
} 静态PhoneClient()
{
客户端=新的HttpClient();
client.MaxResponseContentBufferSize = 256000;
client.DefaultRequestHeaders.Add(用户代理,Mozilla的/ 5.0(兼容; MSIE 10.0; Windows NT的6.2; WOW64;三叉戟/ 6.0));
} 公共异步静态任务<名单,LT;分类>> GetDefaultCategories()
{
HTT presponseMessage的GetResponse =等待client.GetAsync(ServerBaseUri +Categoryss);
JSON字符串等待= getresponse.Content.ReadAsStringAsync();
JSON = json.Replace(< BR>中,Environment.NewLine);
VAR类别= JsonConvert.DeserializeObject<名单,LT;分类>>(JSON);
返回categories.ToList();
}
}
您应该避免异步无效
。我在 MSDN文章解释这一指导方针。
一旦你改变你的异步无效
方法异步任务
:
公共异步任务GetCategoriesAsync()
{
Categorieslist =新的ObservableCollection<类别>(等待PhoneClient.GetDefaultCategories());
}
然后你可以等待
它是这样:
保护覆盖异步无效的OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs E)
{
等待categoriesvm.GetCategoriesAsync();
BookCategories.DataContext =从虚拟机中categoriesvm.Categorieslist选择虚拟机;
}
不过,我建议你做的所有虚拟机初始化的UI事件之外 - 这会让你的code更容易测试。看看我的 异步
构造博客文章的想法。
I am new to async and await style of programming. How can I solve the following problem:
I am calling the below code first. The problem here is that the first line is awaiting which need to populate the categoriesvm.Categorieslist
, which it does not, but the second line is called. (which I think is the default behaviour of await)
How can I make sure that the second line is called only when the categoriesvm.Categorieslist
is populated in the first line?
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
categoriesvm.GetCategories();
BookCategories.DataContext = from vm in categoriesvm.Categorieslist select vm;
}
In the code above when I execute the first line it goes below where Categorieslist
is the list I am accessing above.
public async void GetCategories()
{
Categorieslist = new ObservableCollection<Categories>(await PhoneClient.GetDefaultCategories());
}
The phoneclient
is below
public class PhoneClient
{
private static readonly HttpClient client;
public static Uri ServerBaseUri
{
get { return new Uri("http://169.254.80.80:30134/api/"); }
}
static PhoneClient()
{
client =new HttpClient();
client.MaxResponseContentBufferSize = 256000;
client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
}
public async static Task<List<Categories>> GetDefaultCategories()
{
HttpResponseMessage getresponse = await client.GetAsync(ServerBaseUri + "Categoryss");
string json = await getresponse.Content.ReadAsStringAsync();
json = json.Replace("<br>", Environment.NewLine);
var categories = JsonConvert.DeserializeObject<List<Categories>>(json);
return categories.ToList();
}
}
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.
这篇关于异步执行的期待的Windows Phone 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!