访问控件-缺少using指令或程序集引用

本文关键字:程序集 引用 指令 using 控件 缺少 访问 | 更新日期: 2023-09-27 18:13:08

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

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

WpfApplication1。的定义'mainViewport'和'mainViewport'没有扩展方法接受类型'WpfApplication1 '的第一个参数。可以找到"mainWindow"。(您是否缺少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         
        }
    }
}

访问控件-缺少using指令或程序集引用

您在构造函数中引用Viewport。在那个时刻,Viewport还没有创建。

像这样使用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加载窗口的UI。

据我所知,你在代码中删除了那个调用,这也会导致代码中断。

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

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    }
}

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