我已将所有模型添加为资源词典作为控件模板,我可以使用此代码加载它
<Control Name="control" Width="507" Height="309" Template="{StaticResource SkeletonModel}"
现在我想在运行时编辑该模型,所以我试图获取在控制元素中加载的viewport3d在运行时编辑它,我不能使用触发器,所以我必须得到参考它然后从这里的代码操纵它是我试过的代码
public partial class MainWindow : Window
{
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Viewport3D v = this.GetTemplateChild("ModelView") as Viewport3D;
}
public MainWindow()
{
InitializeComponent();
}
}
v总是= null我还尝试将静态资源更改为动态资源,它没有工作我还尝试了其他几种方式,如
Viewport3D v = control.Template.LoadContent() as Viewport3D;
ModelVisual3D model;
model = FindName("SkeletonHumanModel") as ModelVisual3D;
这会加载视口和模型,但是当我编辑其中任何一个时它都没有被编辑,所以我想loadcontent只是加载一个副本或另一个实例而不是窗口中使用的当前实例 这是资源字典
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
>
<!--x:Class="Dynamic.ModelOperations"
x:ClassModifier="public"-->
<ControlTemplate x:Key="SkeletonModel" >
<Viewport3D Width="500" Height="500" x:Name="ModelView" ClipToBounds="True" >
<Viewport3D.Camera>
<PerspectiveCamera FieldOfView="35" FarPlaneDistance="110.57139405398024" LookDirection="0,0,-38.5951803220261" NearPlaneDistance="0.1" Position="-16.3927650451661,9.1631622056011,38.1892213715065" UpDirection="0,1,0"/>
</Viewport3D.Camera>
<ModelVisual3D x:Name="World">
<ModelVisual3D x:Name="AmbientLightContainer">
<ModelVisual3D.Content>
<AmbientLight x:Name="AmbientLight" Color="#FF7F7F7F"/>
</ModelVisual3D.Content>
</ModelVisual3D>
<ModelVisual3D x:Name="DirectionalLightContainer">
<ModelVisual3D.Content>
<DirectionalLight x:Name="DirectionalLight" Color="#FF3F3F3F" Direction="0,0,-1">
<DirectionalLight.Transform>
<TranslateTransform3D OffsetZ="3" OffsetX="0" OffsetY="0"/>
</DirectionalLight.Transform>
</DirectionalLight>
</ModelVisual3D.Content>
</ModelVisual3D>
<ModelVisual3D x:Name="SkeletonHumanModel" >.....
提前致谢
答案 0 :(得分:0)
要从ResourceDictionary获取资源,您应该使用FindResource()方法。如果您的资源字典在App资源中合并,从MainWindow,您可以获得这样的资源,
private void OnLoaded(object sender, RoutedEventArgs e)
{
var item = this.FindResource("SkeletonModel") as Viewport3D;
}