WPF元素线程分派器错误 [英] Wpf element thread dispatcher error
问题描述
我有一个图片元素,我只是想从后面的代码中更改图片来源.
这是代码:
I have an image element which I am just trying to change the image source from code behind.
This is the code:
rgbImage.Source = new BitmapImage(new Uri("/Images/Play.png", UriKind.Relative));
这是错误异常:
未处理的异常:System.InvalidOperationException:调用线程不能
访问此对象,因为其他线程拥有它.
在System.Windows.Threading.Dispatcher.VerifyAccess()
谁能提供任何想法?
谢谢进阶
我尝试过的事情:
我尝试了几种不同的技术来访问在其上创建了该元素的调度程序,但是为了我自己,我无法执行这一行.我尝试过的:
System.Windows.Application.Current.Dispatcher.Invoke(new Action(()=>
{
rgbImage.Source =新的BitmapImage(新的Uri("/Images/Play.png",UriKind.Relative));
})); //同样的错误
rgbImage.Dispatcher.Invoke(new Action(()=>
{
rgbImage.Source =新的BitmapImage(新的Uri("/Images/Play.png",UriKind.Relative));
})); //冻结应用程序
This is the error exception:
Unhandled Exception: System.InvalidOperationException: The calling thread cannot
access this object because a different thread owns it.
at System.Windows.Threading.Dispatcher.VerifyAccess()
Can anyone provide any ideas?
Thanks in advanced
What I have tried:
I tried several different techniques to access the dispatcher on which this element was created but I cant for the sake of me get this single line executed. What I have tried:
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
{
rgbImage.Source = new BitmapImage(new Uri("/Images/Play.png", UriKind.Relative));
})); // same error
rgbImage.Dispatcher.Invoke(new Action(() =>
{
rgbImage.Source = new BitmapImage(new Uri("/Images/Play.png", UriKind.Relative));
})); // freezes the app
推荐答案
尝试使用BeginInvoke
代替.
Dispatcher.BeginInvoke方法(System.Windows.线程) [
Try usingBeginInvoke
instead.
Dispatcher.BeginInvoke Method (System.Windows.Threading)[^]
rgbImage.Dispatcher.BeginInvoke(new Action(() =>
{
rgbImage.Source = new BitmapImage(new Uri("/Images/Play.png", UriKind.Relative));
}));
如果图像没有立即更新,则说明有某些东西阻塞了UI线程.您是否阻止了UI线程等待后台线程完成?
If the image doesn''t update immediately, then something is blocking the UI thread. Have you blocked the UI thread waiting for a background thread to finish?
这篇关于WPF元素线程分派器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!