【问题标题】:Accessing a control - Missing a using directive or assembly reference访问控件 - 缺少 using 指令或程序集引用
【发布时间】:2013-05-06 16:23:57
【问题描述】:

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

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

'WpfApplication1.mainWindow' 不包含对 '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         
        }
    }
}

【问题讨论】:

  • 您无法通过这种方式访问​​ Viewport3D 控件,因为它位于 Grid 中。您需要通过 Grid 的子控件访问它。使用 FindName() 方法。
  • @ShaiAharoni 完全不正确。太糟糕了,我不能对 cme​​ts 投反对票

标签: c# wpf 3d


【解决方案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)
    {

    }
}

【讨论】:

  • 我明白你想说什么。如果理解得很好,您的意思是让构造函数“公共主窗口(){}”为空并在其他地方编写我的代码?如果是这样,你说的窗口的加载事件处理程序是什么意思?
  • 你说得对,一开始我不明白 InitializeComponent() 是干什么用的,所以我把它删了!非常感谢,现在可以使用了!
【解决方案2】:

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

【讨论】:

  • 谢谢,你是对的!我刚刚更改了它,现在没有出现错误。但是程序仍然无法运行,它说:“没有可用的源 - 没有为任何调用堆栈帧加载任何符号。源代码无法显示”。任何想法如何解决这个问题?
猜你喜欢
  • 2011-02-02
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多