简体   繁体   中英

System.UnauthorizedAccessException: Invalid cross-thread access while updating ListPicker in WP8

I'm building simple WP8 application. I'm trying to update a ListPicker control with a collection that is a member of another class. This class obtains this data with an asynchronous call. When the response is received that triggers an event on the MainPage that updates the ListPicker .

    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());
        }

    }

I'm getting the following exception:

A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll

An exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary System.UnauthorizedAccessException: Invalid cross-thread access. at MS.Internal.XcpImports.CheckThread() at System.Windows.DependencyObject.GetValueInternal(DependencyProperty dp) at System.Windows.FrameworkElement.GetValueInternal(DependencyProperty dp) at System.Windows.Controls.ItemsControl.get_Items() at BitCoinTail.MainPage.PopulateListPicker()

Strangely when I try to access another member of this class with a simple string it works fine. I was developing a little bit in WP7 but don't recall ever getting this exception. Can anyone see what I'm doing wrong?

您需要使用调度程序来封送对适当线程的调用-请参阅http://www.codeproject.com/Articles/368983/Invoking-through-the-Dispatcher-on-Windows-Phone-a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM