简体   繁体   English

在后面的代码中定义 HelixViewport3D 与在 XAML 中不同吗?

[英]Define HelixViewport3D in code behind is diffrent then in XAML?

There is difference when you add ModelVisual3D to HelixViewport3D defined in XAML or in code behind.将 ModelVisual3D 添加到 XAML 或代码隐藏中定义的 HelixViewport3D 时会有所不同。

In my XAML I have:在我的 XAML 中,我有:

    <Grid>
        <HelixToolkit:HelixViewport3D Name="m_helix_viewport">
        </HelixToolkit:HelixViewport3D>
    </Grid>

In code behind:在后面的代码中:

private ModelVisual3D getData() {
    ModelImporter importer = new ModelImporter();
    Model3D model = importer.Load(@"c:\test\test.obj");

    Model3DGroup group = new Model3DGroup();
    group.Children.Add(model);

    ModelVisual3D myModelVisual3D = new ModelVisual3D();
    myModelVisual3D.Content = group;
    return myModelVisual3D;
}

private void Test1() {
    /// do not render
    HelixViewport3D v = new HelixViewport3D();
    v.Children.Add(getData());
    m_helix_viewport = v;
}

private void Test2() {
    /// rendering ok
    m_helix_viewport.Children.Add(getData());
}

Question is why Test1() do not render but Test2() is ok.问题是为什么 Test1() 不渲染但 Test2() 没问题。 IMHO XAML definition is different then code definition - something is missing - but what?恕我直言,XAML 定义与代码定义不同-缺少某些东西-但是什么?

It seems the field m_helix_viewport is just a reference to the actual instance of the control that is bound to the view.似乎字段 m_helix_viewport 只是对绑定到视图的控件的实际实例的引用。 When you assign a new instance of that control to that field the reference is lost and because your new control is not bound to the view that was loaded from xaml changes to the properties of the new control doesn't affect the view.当您将该控件的新实例分配给该字段时,引用将丢失,并且因为您的新控件未绑定到从 xaml 加载的视图,所以对新控件属性的更改不会影响该视图。 If you check out the generated code-behind file there is a method called Connect.如果您查看生成的代码隐藏文件,则有一个名为 Connect 的方法。 In this method the created instances of your controls are assigned to the generated fields.在此方法中,控件的创建实例被分配给生成的字段。 In other words the instances are created by some other code and references are passed to the code-behind class.换句话说,实例是由其他一些代码创建的,并且引用被传递给代码隐藏类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM