2

我在 C# 中使用 WPF 应用程序,我想在开始时绘制一个三角形。

这是我运行程序时出现的错误:

“WpfApplication1.mainWindow”不包含“mainViewport”的定义,并且找不到接受“WpfApplication1.mainWindow”类型的第一个参数的“mainViewport”的扩展方法。(您是否缺少 using 指令或程序集引用?)

这是我的 XAML 页面:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF 3D Chart" Height="455" Width="689">
    <Grid>
        <Viewport3D Name="mainViewport" ClipToBounds="True">
            <Viewport3D.Camera>
                <PerspectiveCamera 
  FarPlaneDistance="100"
  LookDirection="-11,-10,-9"
  UpDirection="0,1,0"
  NearPlaneDistance="1" 
  Position="11,10,9" 
  FieldOfView="70" />
            </Viewport3D.Camera>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <DirectionalLight 
    Color="White" 
    Direction="-2,-3,-1" />
                </ModelVisual3D.Content>
            </ModelVisual3D>
        </Viewport3D>
    </Grid>
</Window>

这是我的代码:(错误出现在我的代码的最后一行)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Media.Media3D;


namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            System.Windows.Media.Media3D.Point3D point0 = new Point3D(-0.5, 0, 0);
            System.Windows.Media.Media3D.Point3D point1 = new Point3D(0.5, 0.5, 0.3);
            System.Windows.Media.Media3D.Point3D point2 = new Point3D(0, 0.5, 0);


            System.Windows.Media.Media3D.MeshGeometry3D triangleMesh = new MeshGeometry3D();

            triangleMesh.Positions.Add(point0);
            triangleMesh.Positions.Add(point1);
            triangleMesh.Positions.Add(point2);

            int n0 = 0;
            int n1 = 1;
            int n2 = 2;

            triangleMesh.TriangleIndices.Add(n0);
            triangleMesh.TriangleIndices.Add(n1);
            triangleMesh.TriangleIndices.Add(n2);

            System.Windows.Media.Media3D.Vector3D norm = new Vector3D(0, 0, 1);
            triangleMesh.Normals.Add(norm);
            triangleMesh.Normals.Add(norm);
            triangleMesh.Normals.Add(norm);

            System.Windows.Media.Media3D.Material frontMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Blue));

            System.Windows.Media.Media3D.GeometryModel3D triangleModel = new GeometryModel3D(triangleMesh, frontMaterial);

            triangleModel.Transform = new Transform3DGroup();

            System.Windows.Media.Media3D.ModelVisual3D visualModel = new ModelVisual3D();
            visualModel.Content = triangleModel;

            this.mainViewport.Children.Add(visualModel); //here appears the error         
        }
    }
}
4

2 回答 2

1

您指的是构造函数中的视口。在那一刻,视口尚未创建。

像这样使用 Window 的 Loaded 事件处理程序

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Loaded="Window_Loaded">
    <Grid>

不要让构造函数为空!里面有一个重要的电话!InitializeComponent 加载 Window 的 UI。

据我所见,您在代码中删除了该调用,这也导致代码中断。使用 Loaded 处理程序,这就是它的用途。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }


    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }
}
于 2013-05-12T14:55:36.170 回答
1

您的 XAML 创建一个名为的类WPFChart.Window1,而您的代码修改一个名为WpfApplication1.MainWindow. 我不知道哪一个是正确的,但是您需要更改其中一个以使它们匹配。

于 2013-05-12T14:44:13.150 回答