我正在WP 8.1上編寫應用程序。我的一個方法是解析html,一切都很好。但是我想改變編碼來打磨波蘭人。 所以我必須將Length屬性設置爲變量類型byte []。爲了使這成爲可能,我需要使用等待並更改我的方法asnych。異步,等待和奇怪的結果
public async void GetTimeTable(string href, int day)
{
string htmlPage = string.Empty;
using (var client = new HttpClient())
{
var response = await client.GetByteArrayAsync(URL);
char[] decoded = new char[response.Length];
for (int i = 0; i < response.Length; i++)
{
if (response[i] < 128)
decoded[i] = (char)response[i];
else if (response[i] < 0xA0)
decoded[i] = '\0';
else
decoded[i] = (char)iso8859_2[response[i] - 0xA0];
}
htmlPage = new string(decoded);
}
// further code... and on the end::
TimeTableCollection.Add(xxx);
}
public ObservableCollection<Groups> TimeTableCollection { get; set; }
方法是從MainPage.xaml.cs中
調用vm.GetTimeTable(navContext.HrefValue, pivot.SelectedIndex);
TimeTableViewOnPage.DataContext = vm.TimeTableCollection;
現在是我的問題。爲什麼vm.TimeTableCollection是null?當我不使用異步並等待一切正常時,vm.TimeTableCollection包含x個元素。
您不等待'GetTimeTable',因此控件在完成之前會繼續到下一行。對此,有很多例子。 – 2014-09-26 14:53:19