首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在非UI线程中填充WP7后,ObservableCollection中的跨线程访问无效

在非UI线程中填充WP7后,ObservableCollection中的跨线程访问无效
EN

Stack Overflow用户
提问于 2011-08-02 15:29:14
回答 1查看 743关注 0票数 0

我正在为WP7中的跨线程操作而挣扎。元素已成功添加,请执行ObservableCollection,但随后不会显示任何内容。数据绑定ListBox (lBox)提供:

无效的跨线程访问。

以下是我所拥有的:

代码语言:javascript
复制
public partial class MainPage : PhoneApplicationPage
{
    private ObservableCollection<string> obrazkiFinal = new ObservableCollection<string>();

    public ObservableCollection<string> ObrazkiFinal
    {
        get { return obrazkiFinal; }
        set { obrazkiFinal = value; }
    }

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        lBox.ItemsSource = ObrazkiFinal;

        HttpWebRequest httpRequest = WebRequest.CreateHttp(@"http://website");
        IAsyncResult res = httpRequest.BeginGetResponse(new AsyncCallback(RespResult),httpRequest);
    }

    private void RespResult(IAsyncResult respResylt)
    {
        var res = (HttpWebRequest)respResylt.AsyncState;
        var resp = res.EndGetResponse(respResylt);

        /* some parsing code */
         foreach (/* found pic urls */)
         {
             //new httpwebrequest
             HttpWebRequest picHttpRequest = WebRequest.CreateHttp(picUrl);
             IAsyncResult picRes = picHttpRequest.BeginGetResponse(DownloadImageResult, picHttpRequest );

       }

    private void DownloadImageResult(IAsyncResult result)
    {
        var res = state.HttpWebRequest;
        var resp = res.EndGetResponse(result);

        /*some saving code*/

        Dispatcher.BeginInvoke(() => { ObrazkiFinal.Add(fileName); });
     }
    }
}

然后在XAML中:

代码语言:javascript
复制
<ListBox Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="Auto" Name="lBox">
 <ListBox.ItemTemplate>
     <DataTemplate>
        <StackPanel>
              <Image Source="{Binding}"></Image>
        </StackPanel>
     </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

当然,在PhoneApplicationPage中:

代码语言:javascript
复制
DataContext="{Binding RelativeSource={RelativeSource Self}}"

在UI线程中成功地填充了ObservableCollection,那么有什么问题呢?

@编辑堆栈跟踪添加:

( System.Windows.DependencyObject.GetValueInternal(DependencyProperty dp) ( System.Windows.FrameworkElement.GetValueInternal(DependencyProperty dp),System.Windows.DependencyObject.GetValue(DependencyProperty dp(System.Windows.DependencyObject.GetValue(DependencyProperty dp),System.Windows.Controls.ItemsControl.get_ItemsSource() at myPhoneApp.MainPage.DownloadImageResult(IAsyncResult (成绩) System.Net.Browser.ClientHttpWebRequest.<>c_DisplayClassa.b_8(Object state2( System.Threading.ThreadPool.WorkItem.WaitCallback_Context( )在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,( System.Threading.ThreadPool.WorkItem.doWork(Object o)在System.Threading.Timer.ring()上的ContextCallback回调,对象状态)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-02 17:43:53

根据堆栈跟踪,DownloadImageResult访问ListBoxItemsSource属性(我假设)。将其移除或移动到BeginInvoke块中。

关于更具体的建议,请张贴您的DownloadImageResult功能的全部内容。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6914533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档