嗨,我正在学习C#WPF MVVM。因此,我有一个具有以下构造函数的类
型号类别
public BillMetaData(bool isKey, string attName, int attType, bool isRequired, int attLoc, int attLength, int isDecimal, int attAlignment) {
_isKey = isKey;
_attName = attName;
_attType = attType;
_isRequired = isRequired;
_attLoc = attLoc;
_attLength = attLength;
_isDecimal = isDecimal;
_attAlignment = attAlignment; }
现在我有了我的ModelView,它还没有与数据库连接,我只是想了解模式。
bool isKey = true;
string attName = "Name";
int attType = 1;
bool isRequired = true;
int attLoc = 1;
int attLength = 30;
int isDecimal = 1;
int attAlignment = 1;
private BillMetaData obj = new BillMetaData( isKey, attName, attType, isRequired, attLoc, attLength, isDecimal, attAlignment);
public string TxtCustomerName
{
get { return obj.attName; }
set { obj.attName = value; }
}
然而,当创建BillMetaData时,我得到了以下错误
a field initializer cannot reference the nonstatic field
在我的ViewModel文件夹中,我引用我的模型如下
using Project.Model;
我做错了什么?
不能在字段初始值设定项中使用实例变量调用参数化构造函数。因此,在字段初始化程序中根本不能使用实例变量。
如果您需要像那样调用构造函数,请在方法中执行(例如,您的构造函数或Init例程)。
public MyViewModel()
{
private BillMetaData obj = new BillMetaData( isKey, attName, attType, isRequired, attLoc, attLength, isDecimal, attAlignment);
}