编译器找不到方法名称

时间:2012-12-14 20:10:20

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

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

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

    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
     }

所以定义可以在上面看到。

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

然后在'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不包含”方法和的定义   没有扩展方法“方法”接受类型的第一个参数   可以找到ProjectName.className(你是否错过了使用   指令或汇编参考“

任何人都可以通过此信息确定我的问题所在吗?

我编辑了这个以显示其他名称空间声明,首先是“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>

2 个答案:

答案 0 :(得分:4)

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

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

此外,您应该使用Pascal大小写作为类名,而FTLOG则将您的类命名为“className”以外的其他类。

答案 1 :(得分: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的实际命名空间,但我怀疑它是命名空间不匹配。