1

我有一个有两个字段和一些绘图方法的类:

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内部使用DrawSomething()- 没有帮助。如何使其工作成为可能?提前感谢您的帮助!

[编辑 1.]
也许我应该使用不同于DrawingGroup?

[编辑 2.]
没人知道吗?:(

4

1 回答 1

0

好的,我找到了解决方案......这真的很明显......我必须在主线程中(在主窗口构造函数中)初始化整个绘图工具,然后设置MyImage.Source(也在同一个地方)。现在一切都会好起来的!:)

于 2013-01-07T21:15:12.943 回答