Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF:如何将xaml文件中的3D对象添加到我的viewport3D?_Wpf_Xaml - Fatal编程技术网

WPF:如何将xaml文件中的3D对象添加到我的viewport3D?

WPF:如何将xaml文件中的3D对象添加到我的viewport3D?,wpf,xaml,Wpf,Xaml,我有viewport3D,我想将xaml文件中定义的多个3d对象添加到此viewport3D,但默认情况下,.xaml文件中定义的3d对象已随viewport3D一起提供,我无法将其添加到我的viewport3D 如何使用Viewport3D中.xaml文件中的这些3D对象 我尝试使用XamlReader读取xaml,将其转换为viewport3D,然后获取vieport3D子元素并添加到我的视口中, 但我得到一个错误“指定的Visual已经是另一个Visual的子对象或CompositionT

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

我尝试使用XamlReader读取xaml,将其转换为viewport3D,然后获取vieport3D子元素并添加到我的视口中,
但我得到一个错误“指定的Visual已经是另一个Visual的子对象或CompositionTarget的根对象。”

请尝试下面的代码。我没有在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);
}
Viewport3D vp=XamlReader.Load(文件名)为Viewport3D;
//将子图形移动到临时集合。
列表项=新列表();
foreach(副总裁儿童中的Visual3D视觉)
{
项目。添加(视觉);
}
//将视觉效果与原始父对象“分离”。
副总统,儿童,清除();
//现在,将其放在“目标”视口中。
foreach(项目中的var项目)
{
myViewport.Children.Add(项);
}

尝试下面的代码。我没有在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);
}
Viewport3D vp=XamlReader.Load(文件名)为Viewport3D;
//将子图形移动到临时集合。
列表项=新列表();
foreach(副总裁儿童中的Visual3D视觉)
{
项目。添加(视觉);
}
//将视觉效果与原始父对象“分离”。
副总统,儿童,清除();
//现在,将其放在“目标”视口中。
foreach(项目中的var项目)
{
myViewport.Children.Add(项);
}