[英]Can't access base properties in derived class (C#)
我在C#/ Xamarin.Forms中继承/多态性遇到麻烦。 我认为有很多事情可以允许我做,但是我没有,从我的收集来看,我似乎没有正确继承,所以首先要做的是:
public abstract partial class CommonCell : ContentView, ICellSection
{
public static readonly BindableProperty TitleProperty = BindableProperty.Create("Title", typeof(string), typeof(CommonCell), default(string));
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
public CommonCell()
: base()
{
}
public virtual IInputType GetInputType()
{
throw new NotImplementedException();
}
}
在同一文件中,我还声明了这个派生类:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CellSection<T>: CommonCell where T : View, IInputType
{
private T _inputType;
public T InputType
{
get { return _inputType; }
set { _inputType = value; } //plus more stuff here
}
public CellSection ()
:base()
{
//Layout Initialization
}
public override IInputType GetInputType() //I get an error here (Problem 2)
{
return InputType;
}
}
我只包含了一个属性和一种方法来演示我的问题,但是还有更多的声明方式相同...
发生的是,当我创建一个新的CellSection时,如下所示:
CellSection<TextInput> section1 = new CellSection<TextInput>();
section1.Title = "New Title"; //This line is not allowed
我不允许访问Title属性...
错误消息: CellSection<TextInput>' does not contain a definition for 'Title'
通过将以下代码添加到CellSection<T>
类中,我已经能够访问它:
public string Title
{
get { return base.Title; }
set { base.Title = value; }
}
但这似乎是该死的不必要和多余的...必须有另一种方式...
我GetInputType()
的另一个问题是不允许覆盖虚拟方法GetInputType()
,它也在上面的代码中,但这是我所谈论的这一行:
public override IInputType GetInputType()
{
return InputType;
}
错误消息: 'CellSection<T>.GetInputType()': no suitable method found to override
最终的问题是,当我来自不同的类/文件时,尝试将CellSection<T>
引用/ CellSection<T>
到ICellSection
(由基类CommonCell
)
List<ICellSection> Sections = new List<ICellSection> { section1, section2 };
我收到不允许的错误消息。
错误消息: Argument 1: cannot convert from 'CellSection<TextInput>' to 'ICellSection'
我不确定这一点,因为我不知道它是否可能(无法找到示例),但是问题1和2我只是不明白为什么它们不起作用,因为它们看起来很简单向前...
每位客户支持员工:
“你试过把它关掉再打开吗?”
我:
“该死...是的,是的,我已将其关闭然后再打开”
原来,这个问题是Visual Studio或Xamarin错误。 我删除了所有相关文件并再次添加了它们(将相同的旧代码复制并粘贴到新文件中)。 繁荣! 现在一切都已正确继承。 我不知道是什么引起了这个问题,因为我认为这是.projitems文件中的参考问题,但是对该文件基本上没有任何更改(在git中检查几行切换位置,仅此而已)。
如果将来还有其他人遇到相同或类似的问题:在遇到此问题时,我多次重新启动了Visual Studio。 遇到此问题时,我至少重启了计算机一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.