我似乎遇到了我创建的课程的问题。当我尝试使用以下代码在wpf窗口的xaml文件中创建它的实例时,我收到错误“type has no accessible constructors”:
<local:TextStyleDefinition x:Key="deleteItem" StyleName="Delete Notes" ItsId="-1"/>
TextStyleDefinition类定义如下:
public class TextStyleDefinition : BaseStyleDefinition
{
public string TextFontName { get; set; }
public string TextSize { get; set; }
public string TextTabSize { get; set; }
public int TextBold { get; set; }
public int TextItalic { get; set; }
public int TextUnderline { get; set; }
public string TextWidthFactor { get; set; }
public TextStyleDefinition() : base()
{
System.Diagnostics.Debug.WriteLine("TSD ctor : blank");
}
// override object.Equals
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
TextStyleDefinition lsd = obj as TextStyleDefinition;
if (this.StyleName != lsd.StyleName ||
this.ItsId != lsd.ItsId)
return false;
return true;
}
// override object.GetHashCode
public override int GetHashCode()
{
return ItsId;
}
}
因此BaseStyleDefinition类是:
public abstract class BaseStyleDefinition : INotifyPropertyChanged
{
public string StyleName { get; set; }
public int ItsId { get; set; }
public BaseStyleDefinition() : base() { }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
// override object.Equals
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
return false;
BaseStyleDefinition rsd = obj as BaseStyleDefinition;
if (this.StyleName != rsd.StyleName ||
this.ItsId != rsd.ItsId)
return false;
return true;
}
// override object.GetHashCode
public override int GetHashCode()
{
return ItsId;
}
}
正如你所看到的,我有无参数构造函数,类是公共的,所以它不是明显的问题。代码编译正常并正确执行,但VS2015将xaml中的行突出显示为错误,并且不会在wpf设计器中显示该窗口。
有没有猜到它会是什么?