Invalid cross-thread access issue

前端 未结 3 969
暖寄归人
暖寄归人 2020-12-01 04:07

I have two ViewModel classes : PersonViewModel and PersonSearchListViewModel. One of the fields PersonViewModel implements is a profile image that is downloaded via WCF(cach

3条回答
  •  醉话见心
    2020-12-01 04:24

    It can be achieved with WriteableBitmap.

        public void LoadThumbAsync(Stream src, 
                        WriteableBitmap bmp, object argument)  
        {  
            ThreadPool.QueueUserWorkItem(callback =>  
            {  
                bmp.LoadJpeg(src);  
                src.Dispose();  
                if (ImageLoaded != null)  
                {  
                    Deployment.Current.Dispatcher.BeginInvoke(() =>  
                    {  
                        ImageLoaded(bmp, argument);  
                    });  
                }  
            });  
        }
    

    But You have to construct WriteableBitmap in UI Thread, then loading can be performed in other thread.

        void DeferImageLoading( Stream imgStream )  
        {  
            // we have to give size  
            var bmp = new WriteableBitmap(80, 80);  
            imageThread.LoadThumbAsync(imgStream, bmp, this);  
        }  
    

    See more explanaition on this blog post

提交回复
热议问题