本文介绍了WPF:如何将 3D 对象从 xaml 文件添加到我的 viewport3D?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 viewport3D 并且我想将在 xaml 文件中定义的多个 3d 对象添加到此 Viewport3D,但默认情况下,在 .xaml 文件中定义的 3d 对象已经带有 viewport3D,我无法将其添加到我的 Viewport3D如何在 Viewport3D 中使用 .xaml 文件中的那些 3D 对象?

I have viewport3D and I want to add multiple 3d objects defined in xaml files to this Viewport3D, but by default 3d object deffined in .xaml file comes already with viewport3D and I can not add it to my Viewport3DHow I can use those 3D objects from .xaml files in my Viewport3D?

我尝试使用 XamlReader 读取 xaml 将其转换为 viewport3D,然后将 vieport3D 子元素添加到我的视口中,但我收到错误消息指定的 Visual 已经是另一个 Visual 的子项或 CompositionTarget 的根."

I tryed to read xaml with XamlReader cast it to viewport3D and then take vieport3D child elements and add to my viewport, but I get error "Specified Visual is already a child of another Visual or the root of a CompositionTarget."

推荐答案

试试下面的代码.我还没有真正在 WPF 中使用 3D,但我猜它的行为与常见的 WPF 面板相同:

Try the code below. I haven't really worked with 3D in WPF but I'm guessing it behaves the same as the common WPF Panels:

Viewport3D vp = XamlReader.Load(fileName) as Viewport3D;

//Move the child visuals to a temporary collection.
List<Visual3D> items = new List<Visual3D>();
foreach (Visual3D visual in vp.Children)
{
    items.Add(visual);
}

//"Detach" the visuals from the original parent.
vp.Children.Clear();

//Now, put it in the destination viewport.
foreach (var item in items)
{
    myViewport.Children.Add(item);
}

这篇关于WPF:如何将 3D 对象从 xaml 文件添加到我的 viewport3D?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 08:04