WPF 中的图像更新时发生 TargetInitationException
我构建了一个显示图像的 WPF 控件。现在我想以非常快的速度改变这个形象。 我构建了一个 ImageContainer 类,它保存图像并有一个 ChangedEventHandler ,它在更改时更新我的控件中的图像。
执行的代码如下所示:
videoImageThread = new Thread(
new ThreadStart(
delegate()
{
this.VideoCapture.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
videoImage.Source = VideoImageContainer.Instance.VideoBitmapSourceImage;
}
));
}
));
private void Instance_VideoRefresh()
{
if (VideoImageContainer.Instance.VideoImage != null)
{
lock (videoImageSetLock)
{
videoImageThread.Start();
}
}
}
此代码抛出 System.Reflection.TargetInvocableException,我做错了什么?
I've built a WPF Control which displays an Image. Now I would like to change that image at a very fast rate.
I've build an ImageContainer class which holds the image and has a ChangedEventHandler which updates the Image in my control when changed.
The code which is executed looks like this:
videoImageThread = new Thread(
new ThreadStart(
delegate()
{
this.VideoCapture.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
videoImage.Source = VideoImageContainer.Instance.VideoBitmapSourceImage;
}
));
}
));
private void Instance_VideoRefresh()
{
if (VideoImageContainer.Instance.VideoImage != null)
{
lock (videoImageSetLock)
{
videoImageThread.Start();
}
}
}
This code throws a System.Reflection.TargetInvocationException, what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,您正在调用一个线程来调用一个线程?!
您是否尝试过直接调用调度程序上的操作,如下所示:
seems to me like you are invoking a thread to invoke a thread ?!
have you tried invoking the action on the dispatcher directly like so:
您是否尝试过简单地将 videoImage.Source 绑定到属性,并在 Instance_VideoRefresh 方法中更改该属性?
我之前已经尝试过使用 Image/List/Timer 组合,效果非常好。
Have you tried simply binding videoImage.Source to a property, and changing that property in your Instance_VideoRefresh method?
I've tried it before with an Image/List<ImageSource>/Timer combination, and it works pretty well.