【问题标题】:Compiler cannot find method name编译器找不到方法名称
【发布时间】:2012-12-14 20:10:19
【问题描述】:

正在使用 C# 和 XAML 为 Windows 8 制作应用程序。

我在名为 AppNamespace 的命名空间中定义了两个类,其中一个类在其构造函数中采用一个 3 维布尔数组:

    public class Solver
    {
        // Board is a class also in this namespace but I don't think the issue is 
        //there, so I've omitted it's definition
        private Board current;

        // You can see that the class clearly DOES take a parameter of the same type
        // as the array "content" (which is defined later)
        public Solver (bool[, ,] initial)
        {
            // The parameter is then used to construct a "Board" class within the
            // "Solver" class.
            current = new Board(initial);
        }

        // Several methods within the class
     }

所以定义见上。

然后,我在名为“NewPage.xaml”的 gridapp 上创建了一个新页面,在该页面中有一些文本框可以操作数组中的值。 (这里似乎没有问题)

然后在'NewPage.xaml.cs'中单击按钮时,它应该在类上创建一个实例,然后在类中运行一个方法,在我定义的“NewPage.xaml.cs”的顶部如下所示的三维数组,

    // Declare the namespace where the class "Solver" is situated
    using App.Classes;

    namespace App
    {
        // So below is the C# part of the page "PuzzleSolver"
        public sealed partial class PuzzleSolver : App.Common.LayoutAwarePage
        { 
            // Create an array called content
            bool[, ,] content = new bool[9, 9, 9];

            public PuzzleSolver()
            {
                this.InitializeComponent();

                //Set every cell of the created content array to true
                for (int i = 0; i <= 8; i++)
                {
                    for (int j = 0; j <= 8; j++)
                    {
                        for (int k = 0; k <= 8; k++)
                        {
                            content[i, j, k] = true;
                        }
                    }
                }
            }

      /* There are some methods which change information in the array "Content" based on the
         stuff input into the XAML textboxes on the page. */


      // The below method is invoked when a XAML Button is clicked on the page
      // and I intend it to create a "Solver" object, using the "content" array
      // from this page as a parameter
      private void Button_Click_1(object sender, RoutedEventArgs e)
      {
        // So now I create the and pass in the "content" array
        Solver newSolve = new Solver(content);

        newSolve.Solve();
      }
}

问题是,编译器识别出“className”是一个类,但说它没有带 1 个参数的构造函数,它还说:

"ProjectName.className 不包含"method and 没有扩展方法“方法”接受类型的第一个参数 可以找到 ProjectName.className(您是否缺少 using 指令或程序集引用”

谁能从这些信息中找出我的问题出在哪里?

我已经编辑了这个以显示其他命名空间声明,首先是“Solver.xaml.cs”上的声明:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using App.Classes;

现在关于“Solver.xaml”的声明:

<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="App.Solver"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App"
    xmlns:common="using:App.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <x:String x:Key="PageName">Solver</x:String>
    </Page.Resources>

【问题讨论】:

  • 我没有看到您的 className 定义中定义了任何方法()。检查您尝试调用的方法是否存在。
  • '三维布尔数组' - 我的头疼 ;-)
  • @ScottChapman 抱歉,是的,我已经定义了一个方法,但是我把它省略了,因为类本身真的很长而且很复杂,但我不认为问题出在 C# 上,是那么可能是这种情况?
  • 似乎很清楚...从您发布的代码中,类 Solver 有一个无参数的构造函数,但是在 Button_Click_1 中,您正在使用期望和参数(内容)的构造函数来新建一个实例
  • 但是 Solver 类确实有一个参数。

标签: c# xaml windows-8 assembly-references


【解决方案1】:

object 是保留关键字,您不能使用它来命名变量。尝试更改名称:

className somethingDifferent = new className(content);
somethingDifferent.method();

此外,您应该使用 Pascal 大小写作为类名,并且 FTLOG 将您的类命名为“className”以外的名称。

【讨论】:

  • 我猜这是一个糟糕的重命名问题。
  • 抱歉,我在发布的代码中更改了类和对象名称……但我现在将发布实际代码。
【解决方案2】:

当你声明你的实例时,你使用object作为变量名。

className object = new className(content);
object.method();

这会导致编译器混淆,因为object 是一个保留关键字(它总是引用System.Object)。

您应该使用更好的名称。但是,如果您愿意,可以使用 @ 来使用保留字:

className anyNonReservedWord = new className(content);
anyNonReservedWord.method();

// Alternatively
className @object = new className(content);
@object.method();

您没有在代码中显示method 的声明,因此很难解读,尽管可能是由于相同的问题。


编辑后编辑:

我在名为 AppNamespace 的命名空间中定义了两个类

您的 PuzzleSolver 类位于命名空间 App,而不是 AppNamespace。虽然您没有显示 Solver 的实际命名空间,但我怀疑这是命名空间不匹配。

【讨论】:

  • 不知道 @ 的事情。我怀疑我永远不会使用它,但很高兴知道。
  • @SystemDown 是的 - 这就是为什么我在 Cory 的出色回复之外添加了一个答案;)
  • 那是我的错,我在加载代码时更改了类和对象的名称。但是我现在已经把实际的类名放在那里了……而且我相信C# 是正确的,这就是我没有发布方法定义的原因。
  • 在检查您的推荐时,我注意到我将 Solver 类命名为与 Page 类相同的名称,所以这是我的一个愚蠢的错误。不过感谢您的帮助。
猜你喜欢
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 2017-07-29
  • 2021-05-01
  • 2021-02-27
  • 1970-01-01
  • 2016-11-12
相关资源
最近更新 更多