0

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

我正在尝试制作一个软件来绘制和操作 3D 几何图形。我已经按照这个示例来了解如何创建 3D 场景并且成功了。

但是,当我尝试将Viewport3Dacanvas放在 XAML 中时(这样我就可以在 UI 中放置其他元素,如按钮来操作 3D 场景),我无法弄清楚为什么Viewport3D没有出现。

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

C# 文件中的 MainWindow 与 XAML 文件中的不同?

这是我得到的窗口:

在此处输入图像描述

这是代码:

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()[...]

    }
}
4

1 回答 1

0

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

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

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

于 2018-07-09T04:33:12.280 回答