该类型不包含任何可访问的构造函数

时间:2013-06-24 19:55:08

标签: c# wpf constructor

当我以这种方式设置构造函数时,为什么会出现编译错误:

  public class Castle
  {
        public Castle (bool mark, string description)
        {
            CastleMarked = mark;
            CastleDescription = description;
        }

        bool CastleMarked {get; set;}
        string CastleDescription {get; set;}
  }

然后以这种方式从其他地方初始化它:

Castle cas1 = new Castle(true,"Stone");

2 个答案:

答案 0 :(得分:2)

可能是因为您尚未实施INotifyPropertyChanged界面。

这是什么:

CastleMarked  {get; set;}

哪里是财产类型?

修改

public

之前添加class

<强> EDIT2:

你有没有检查过,或者你只是在编辑你的问题; p?

因为此代码可以正常工作:

namespace WpfApplication1
{
    public class Castle
    {
        public Castle(bool mark, string description)
        {
            CastleMarked = mark;
            CastleDescription = description;
        }

        bool CastleMarked { get; set; }
        string CastleDescription { get; set; }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Castle cas1 = new Castle(true, "Stone");
        }
    }
}

答案 1 :(得分:0)

实现添加接口的方法。或者删除Inotifypropertychanged。同时修复private string property { get; set; }

等属性