我建立一个Metro应用。
在MainPage.xaml.cs,我实例专辑如下:
Album album = new Album(2012); //With the album ID as its parameter.
ListView1.ItemsSource = album.Songs;
在Album.cs,构造如下:
public Album(int ID)
{
this.ID = ID;
Initialize(); //Serves as a wrapper because I have to call httpClient.GetStreamAsync() and "async" doesn't work for the constructor.
}
最后,初始化方法:
private async void Initialize()
{
//...some code...
HttpClient cli = new HttpClient();
Stream SourceStream = await HttpClient.GetStreamAsync("http://contoso.com");
//...some code...
this.Songs = Parse(SourceStream);
}
问题是,当它运行到GetStreamAsync,它然后去ListView1.ItemsSource = album.Songs
直接与album.Songs空。
有没有快速的解决这个问题? THX提前。