我正在构建一个 Metro 应用程序。
在 MainPage.xaml.cs 中,我将 Album 实例化如下:
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
直接将专辑.Songs 设为 null。
有什么快速解决这个问题的方法吗?