我有一个有两个字段和一些绘图方法的类:
public class DrawingTool
{
private readonly DrawingGroup _drawingGroup;
private DrawingImage _imageSource;
public DrawingTool()
{
_drawingGroup = new DrawingGroup();
_imageSource = new DrawingImage(_drawingGroup);
}
public DrawingImage GetImageSource()
{
return _imageSource;
}
public void DrawSomething()
{
using (DrawingContext dc = _drawingGroup.Open()) // HERE'S AN ERROR - _drawing group is being used by another thread (InvalidOperationException)
{
// draw something here
}
}
}
这是一个WPF
项目,所以我也有一个Image
:
<Image x:Name="MyImage" Width="320" Height="240"></Image>
我设置了来源:
_drawingTool = new DrawingTool();
Dispatcher.Invoke(() => MyImage.Source = _drawingTool.GetImageSource());
_device.FrameReady += (sender, args) => Dispatcher.Invoke(() =>
{
_drawingTool.DrawSomething();
});
我从评论中收到错误...我尝试调用DrawSomething()
方法并在CheckAccess()
内使用_drawingGroup
进行操作 - 没有帮助。如何让它成功?
提前感谢您的帮助!
[编辑1。]
也许我应该使用与DrawSomething()
不同的东西?
[编辑2。]
没有人有想法? :(
答案 0 :(得分:0)
好的,我找到了解决方案......很明显......我必须在主线程中(在主窗口构造函数中)初始化整个绘图工具,然后设置MyImage.Source
(也在同一个地点)。现在一切都会好起来的! :)