System.UnauthorizedAccessException:在 WP8 中更新 ListPicker 时跨线程访问无效 [英] System.UnauthorizedAccessException: Invalid cross-thread access while updating ListPicker in WP8

查看:16
本文介绍了System.UnauthorizedAccessException:在 WP8 中更新 ListPicker 时跨线程访问无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建简单的 WP8 应用程序.我正在尝试使用属于另一个类的成员的集合来更新 ListPicker 控件.此类通过异步调用获取此数据.当接收到在 MainPage 上触发更新 ListPicker 事件的响应时.

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

    }

我收到以下异常:

System.UnauthorizedAccessException"类型的第一次机会异常发生在 System.Windows.ni.dll

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

System.UnauthorizedAccessException"类型的异常发生在System.Windows.ni.dll 并且在托管/本机之前未处理边界 System.UnauthorizedAccessException: 无效的跨线程使用权.在 MS.Internal.XcpImports.CheckThread() 在System.Windows.DependencyObject.GetValueInternal(DependencyProperty)dp) 在System.Windows.FrameworkElement.GetValueInternal(DependencyPropertydp) 在 System.Windows.Controls.ItemsControl.get_Items() 在BitCoinTail.MainPage.PopulateListPicker()

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

奇怪的是,当我尝试用一​​个简单的字符串访问这个类的另一个成员时,它工作正常.我在 WP7 中进行了一些开发,但不记得曾经遇到过这个异常.谁能看出我做错了什么?

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-电话-a

You need to use a dispatcher to marshal a call to the proper thread - see http://www.codeproject.com/Articles/368983/Invoking-through-the-Dispatcher-on-Windows-Phone-a

这篇关于System.UnauthorizedAccessException:在 WP8 中更新 ListPicker 时跨线程访问无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆