2010-08-26 44 views
1

我有viewport3D,我想将xaml文件中定义的多个3d对象添加到此Viewport3D,但默认情况下,.xaml文件中的3d对象已与viewport3D一起提供,我无法将其添加到我的Viewport3D 如何使用这些我的Viewport3D中的.xaml文件中的3D对象?WPF:如何将3D对象从xaml文件添加到我的viewport3D?

我tryed阅读XAML与XamlReader将它转换为的Viewport3D,然后采取vieport3D子元素,并添加到我的口中, 但我得到的错误“规定的目视已是另一种视觉或CompositionTarget根的一个孩子。”

回答

1

请尝试下面的代码。我没有真正在WPF中使用3D,但我猜它的行为与普通的WPF面板相同:

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