WPF:Canvas (XAML) 内的 Viewport3D (C#)

WPF: Viewport3D (C#) inside a Canvas (XAML)

我是 WPF 的初学者,所以我不完全了解 C# 如何与 XAML 一起制作应用程序。

我正在尝试制作一个软件来绘制和操作 3D 几何图形。我已按照 this example 了解如何创建 3D 场景并取得成功。

但是,当我尝试将 Viewport3D 放在 XAML 中的 canvas 中时(这样我就可以在 UI 中放置其他元素,例如按钮以进行操作3D 场景),我不明白为什么 Viewport3D 没有出现。

我不想在 XAML 中定义 Viewport3D 的所有内容,因为我计划进行更复杂的绘图和几何操作操作。而且我认为最好用 C# 来完成。

C# 文件中的 MainWindow 与 XAML 文件中的不一样?

这是 window 我得到的:

代码如下:

XAML

<Window x:Name="Geology3D_MainWindow" x:Class="Geology3D.MainWindow"
        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"
        xmlns:local="clr-namespace:Geology3D"
        mc:Ignorable="d"
        Title="Geology3D" Height="600" Width="800" Background="Silver">
    <Canvas x:Name="Canvas3D" Background="White" Margin="10,29,16.6,7.4">
        <Viewport3D x:Name="ViewPort3D">

        </Viewport3D>
    </Canvas>
</Window>

C#

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Geology3D
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //Viewport3D ViewPort3D = new Viewport3D();

            Model3DGroup ModelsGroup = new Model3DGroup();
            ModelVisual3D GModelVisual3D = new ModelVisual3D();

            GeometryModel3D CubeGeometryModel = new GeometryModel3D();
            GeometryModel3D PlaneGeometryModel = new GeometryModel3D();

            PerspectiveCamera Camera = new PerspectiveCamera();

            // Specify where in the 3D scene the camera is.
            Camera.Position = new Point3D(-5, 5, -5);

            // Specify the direction that the camera is pointing.
            Camera.LookDirection = new Vector3D(1, -1, 1);

            // Asign the camera to the viewport
            ViewPort3D.Camera = Camera;

            // Define the lights cast in the scene. Without light, the 3D object cannot 
            // be seen. Note: to illuminate an object from additional directions, create 
            // additional lights.
            DirectionalLight GDirectionalLight = new DirectionalLight();
            GDirectionalLight.Color = Colors.White;
            GDirectionalLight.Direction = new Vector3D(-1, -1, -1);

            ModelsGroup.Children.Add(GDirectionalLight);

            // The material specifies the material applied to the 3D object.
            // Define material and apply to the mesh geometries.
            DiffuseMaterial BlueMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.DodgerBlue));
            DiffuseMaterial PlaneMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Gray));

            CubeGeometryModel.Material = BlueMaterial;
            PlaneGeometryModel.Material = PlaneMaterial;

            // Apply the mesh to the geometry model.
            CubeGeometryModel.Geometry = DrawCube();
            PlaneGeometryModel.Geometry = DrawPlane();

            // Apply a transform to the object. In this sample, a rotation transform is applied,  
            // rendering the 3D object rotated.
            RotateTransform3D GRotateTransform3D = new RotateTransform3D();
            AxisAngleRotation3D GAxisAngleRotation3d = new AxisAngleRotation3D();
            GAxisAngleRotation3d.Axis = new Vector3D(0, 1, 0);
            GAxisAngleRotation3d.Angle = 0;
            GRotateTransform3D.Rotation = GAxisAngleRotation3d;
            CubeGeometryModel.Transform = GRotateTransform3D;

            // Add the geometry model to the model group.
            ModelsGroup.Children.Add(CubeGeometryModel);
            ModelsGroup.Children.Add(PlaneGeometryModel);

            // Add the group of models to the ModelVisual3d.
            GModelVisual3D.Content = ModelsGroup;

            ViewPort3D.Children.Add(GModelVisual3D);
        }

        MeshGeometry3D DrawCube()[...]

        MeshGeometry3D DrawPlane()[...]

    }
}

如果打算覆盖 UI 元素,则将 canvas 和 Viewport3D 嵌套在网格中可能更容易。

<Grid>
    <Viewport3D>
    ...
    </Viewport3D>
    <Canvas/>
</Grid>

这样,视口和 canvas 将始终具有相同的大小,并且添加到 canvas 的元素将始终位于顶部。