我正在尝试创建一个显示动画3D模型的wpf应用程序。我有可用的代码在BuildWave中创建模型(我知道这与我以前使用过的一样)。但是,当我将代码移动到wpf窗口类中的后台工作程序时,出现错误“调用线程无法访问此对象,因为另一个线程拥有它”。

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        e.Result = BuildWave();
    }

    private void worker_Completed(object sender, RunWorkerCompletedEventArgs e)
    {
        GeometryModel3D model = (GeometryModel3D)e.Result;
        mGeometry = model.Clone();    //mGeometry is a private member of the window
        if (group.Children.Contains(mGeometry))   //error is on this line
        {
            group.Children.Remove(mGeometry);    //group is a Model3DGroup added in xaml
        }
        group.Children.Add(mGeometry);
        System.Diagnostics.Debug.WriteLine("Added geometry to group");
    }


我已经搜索了有关此问题的解决方案,并找到了一篇文章,该文章涵盖了具有相同错误(The calling thread cannot access this object because a different thread owns it)的问题,建议使用Dispatcher.Invoke()。但是,当我尝试这样做时:

    private void worker_Completed(object sender, RunWorkerCompletedEventArgs e)
    {
        GeometryModel3D model = (GeometryModel3D)e.Result;
        mGeometry = model.Clone();

        group.Dispatcher.Invoke(() =>
        {
            if (group.Children.Contains(mGeometry))
            {
                group.Children.Remove(mGeometry);
            }
            group.Children.Add(mGeometry);    //error is now thrown on this line
        });

        System.Diagnostics.Debug.WriteLine("Added geometry to group");
    }


这将引发错误“不能使用与其父Freezable属于不同线程的DependencyObject。”,并且再次出现一则帖子,内容涉及类似的问题(Cannot use a DependencyObject that belongs to a different thread than its parent Freezable),建议冻结在BuildWave中完成的模型:

        GeometryModel3D model = new GeometryModel3D(WaveMesh, new DiffuseMaterial(Brushes.YellowGreen));
        model.Transform = new Transform3DGroup();

        model.Freeze();
        return model;


我应该怎么做才能解决这个问题?

先感谢您。

最佳答案

在UI线程中调用如下所示:

public static Model3DCollection GetModel3DCollectionByTemplates(List<GeometryTemplate> geometryTemplates) {
        var modelCollection = new Model3DCollection();
        foreach (var geometryTemplate in geometryTemplates) {
            var positions = geometryTemplate.Positions;
            var indicies = geometryTemplate.Indicies;
            var normals = geometryTemplate.Normals;
            var meshGeometry3D = new MeshGeometry3D {
                Positions = new Point3DCollection(positions),
                TriangleIndices = new Int32Collection(indicies),
                Normals = new Vector3DCollection(normals)
            };
            var geometry = new GeometryModel3D(meshGeometry3D,
                new DiffuseMaterial(new SolidColorBrush(Colors.Aquamarine)));
            modelCollection.Add(geometry);
        }
        // =======================
        return modelCollection;
    }


但是在Task(TPL)中返回GeometryTemplate的列表。
和简单的数据包装器类:

public class GeometryTemplate {
    public List<Point3D> Positions { get; private set; }
    public List<Int32> Indicies { get; private set; }
    public List<Vector3D> Normals { get; private set; }

    public GeometryTemplate(MeshGeometry3D meshGeometry3D) {
        SetPositions(meshGeometry3D.Positions);
        SetIndicies(meshGeometry3D.TriangleIndices);
        SetNormals(meshGeometry3D.Normals);
    }

    private void SetNormals(Vector3DCollection normals) {
        Normals = new List<Vector3D>(normals);
    }

    private void SetIndicies(Int32Collection triangleIndices) {
        Indicies = new List<Int32>(triangleIndices);
    }

    private void SetPositions(Point3DCollection positions) {
        Positions = new List<Point3D>(positions);
    }
}


最后一点:

_model3DGroupContainer.Children = model3DCollection;


_model3DGroupContainer是Model3DGroup

关于c# - 将GeometryModel3D添加到Model3DGroup,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42468878/

10-12 03:58
查看更多