开发者

WPF: How to add 3D object from xaml file to my viewport3D?

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 Viewport3D How I can use those 3D objects from .xaml files in my Viewport3D?

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."


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜