无法访问派生类(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; } } 我只包含一个属性和一个方法来演示我的问题,但还有更多的声明以同样的方式… 问题1 当我创建一个新的CellSection时会发生什么,如下所示: CellSection<TextInput> section1 = new CellSection<TextInput>(); section1.Title = "New Title"; //This line is not allowed 我不允许访问Title属性… 错误消息:CellSection< TextInput>‘不包含’Title’的定义 我已经能够通过将以下代码添加到CellSection< T>来访问它了.类: public string Title { get { return base.Title; } set { base.Title = value; } } 但这似乎是多么不必要和冗余……必须有另一种方式…… 问题2 我遇到的另一个问题是我不允许覆盖虚方法GetInputType(),它也在上面的代码中,但这就是我所说的那一行: public override IInputType GetInputType() { return InputType; } 错误消息:’CellSection< T> .GetInputType()’:找不到合适的方法来覆盖 问题3 最后一个问题是,当我来自不同的类/文件时,尝试引用/转换CellSection< T>到ICellSection(由基类CommonCell实现) List<ICellSection> Sections = new List<ICellSection> { section1,section2 }; 我得到一个不允许的错误. 错误消息:参数1:无法从’CellSection< TextInput>‘转换’ICellSection’ 这个我更不确定,因为我不知道它是否可能(无法找到示例),但问题1和2我只是不明白为什么不起作用,因为它们看起来很直向前… 解决方法
每位客户支持员工:
我:
原来这个问题是Visual Studio或Xamarin错误.我删除了所有相关文件并再次添加(将相同的旧代码复制并粘贴到新文件中).繁荣!一切都现在正确地继承.我不知道是什么导致了这个问题,因为我认为它会是.projitems文件中的参考问题,但基本上没有对该文件进行任何更改(检查git几行切换位置,但就是这样). 如果其他人在将来遇到相同或类似的问题:我在遇到此问题时多次重启Visual Studio.遇到这个问题我至少重启过一次机器. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |