预期的类,委托,枚举,接口或结构错误

时间:2014-10-02 05:11:39

标签: c# windows-phone-8 delegates

我试图将此代码输出以供显示,但它给了我这个错误。有人能帮忙吗? 只要我知道哪里出错了,提示和建议就足够了。

public ObservableCollection<PData> Data = new ObservableCollection<PData>()  
        {

                new PData() { title = "slice #1", value = 30 },  
                new PData() { title = "slice #2", value = 60 },  
                new PData() { title = "slice #3", value = 40 },  
                new PData() { title = "slice #4", value = 10 },

        };

            private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)  
            {  
                pie1.DataSource = Data;  
            }  


    public class PData  
    {  
        public string title { get; set; }  
        public double value { get; set; }  
    }  

2 个答案:

答案 0 :(得分:1)

此错误通常意味着您的代码中某处包含错误数量的括号{},或者您的代码不在某个类中(部分?)。

在您的情况下(如果确实是您的所有代码),以下内容:

public ObservableCollection<PData> Data (...) 

不在class { ... }范围内。

如果您的代码多于显示的代码,并且 在一个类中,请查找具有太多近括号}的类或方法定义,这可以解释为“ 课程的结尾“。

简单的例子:

class Stuff{

    public void DoStuff(){
        ...
    }
    } // accidental extra bracket ends the class Stuff too early

    // Error will show here, since this appears to be outside the class:
    public void DoSomethingElse(){

    }

} // The inteded end of Stuff to be

答案 1 :(得分:0)

public class NameList : ObservableCollection<PersonName>
{
    public NameList() : base()
    {
        Add(new PersonName("Willa", "Cather"));
        Add(new PersonName("Isak", "Dinesen"));
        Add(new PersonName("Victor", "Hugo"));
        Add(new PersonName("Jules", "Verne"));
    }
  }

  public class PersonName
  {
      private string firstName;
      private string lastName;

      public PersonName(string first, string last)
      {
          this.firstName = first;
          this.lastName = last;
      }

      public string FirstName
      {
          get { return firstName; }
          set { firstName = value; }
      }

      public string LastName
      {
          get { return lastName; }
          set { lastName = value; }
      }
  }