声明
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我只是不明白为什么不起作用,因为它们看起来很直向前…
解决方法
“Have you tried turning it off and on again?”
我:
“dam’it… Yes,yes I have turned it off and on again”
原来这个问题是Visual Studio或Xamarin错误.我删除了所有相关文件并再次添加(将相同的旧代码复制并粘贴到新文件中).繁荣!一切都现在正确地继承.我不知道是什么导致了这个问题,因为我认为它会是.projitems文件中的参考问题,但基本上没有对该文件进行任何更改(检查git几行切换位置,但就是这样).
如果其他人在将来遇到相同或类似的问题:我在遇到此问题时多次重启Visual Studio.遇到这个问题我至少重启过一次机器.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。