我建立簡單的WP8應用更新ListPicker無效跨線程訪問。我試圖更新ListPicker
控制用的集合,是另一個類的成員。這個類通過異步調用獲取這些數據。當接收到響應觸發MainPage執行該更新ListPicker
的事件。System.UnauthorizedAccessException的:而在WP8
public void coinUtil_ReceivedPriceEvent(object sender, EventArgs e)
{
PopulateListPicker();
}
public void PopulateListPicker()
{
try
{
foreach (KeyValuePair<string, double> item in coinUtil.cointPriceList)
{
listPickerCurrencies.Items.Add(item.Key);
}
}
catch (UnauthorizedAccessException ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
,我發現了以下異常:
類型的第一次機會異常 'System.UnauthorizedAccessException的' 發生在System.Windows.ni.dll
類型的異常「System.UnauthorizedAccessException的」發生在 System.Windows.ni.dll和管理/本地 邊界System.UnauthorizedAccessException的前沒有處理:無效的跨線程 訪問。在MS.Internal.XcpImports.CheckThread()在 System.Windows.DependencyObject.GetValueInternal(的DependencyProperty DP)處System.Windows.Controls.ItemsControl.get_Items System.Windows.FrameworkElement.GetValueInternal(的DependencyProperty DP)()在 BitCoinTail.MainPage.PopulateListPicker()
奇怪的是,當我嘗試訪問該類別的另一成員用一個簡單的字符串,它工作正常。我在WP7中開發了一點,但不記得曾經有過這種例外。任何人都可以看到我做錯了什麼?
我認爲你需要使用調度,以確保UI更新UI線程。 – EricLaw 2013-02-26 22:04:35