简体   繁体   English

无法访问派生类(C#)中的基本属性

[英]Can't access base properties in derived class (C#)

I'm having trouble with inheritance/polymorphism in C#/Xamarin.Forms. 我在C#/ Xamarin.Forms中继承/多态性遇到麻烦。 There's multiple things I'd think I would be allowed to do, but I'm not, and from what I gather it seems I'm not inheriting correctly, so first things first: 我认为有很多事情可以允许我做,但是我没有,从我的收集来看,我似乎没有正确继承,所以首先要做的是:

Declarations 声明书

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();
        }
}

In the same file i also declare this derived class: 在同一文件中,我还声明了这个派生类:

[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;
        }
}

I've only included one property and one method to demonstrate my problem, but there are many more declared the same way... 我只包含了一个属性和一种方法来演示我的问题,但是还有更多的声明方式相同...

Problem 1 问题1

What happens is that when I create a new CellSection, like this: 发生的是,当我创建一个新的CellSection时,如下所示:

CellSection<TextInput> section1 = new CellSection<TextInput>();
section1.Title = "New Title"; //This line is not allowed

I'm not allowed to access the Title property... 我不允许访问Title属性...

Error message: CellSection<TextInput>' does not contain a definition for 'Title' 错误消息: CellSection<TextInput>' does not contain a definition for 'Title'

I've been able to access it by adding the following code to the CellSection<T> class: 通过将以下代码添加到CellSection<T>类中,我已经能够访问它:

public string Title
    {
        get { return base.Title; }
        set { base.Title = value; }
    }

but this seems so damn unnecessary and redundant... There must be another way... 但这似乎是该死的不必要和多余的...必须有另一种方式...

Problem 2 问题2

The other problem I have is that I'm not allowed to override the virtual method GetInputType() , it is in the code above as well, but this is the line I'm talking about: GetInputType()的另一个问题是不允许覆盖虚拟方法GetInputType() ,它也在上面的代码中,但这是我所谈论的这一行:

public override IInputType GetInputType()
{
    return InputType;
}

Error message: 'CellSection<T>.GetInputType()': no suitable method found to override 错误消息: 'CellSection<T>.GetInputType()': no suitable method found to override

Problem 3 问题3

The final problem is that when I'm, from a different class/file, attempt to reference/cast a CellSection<T> to ICellSection (which is implemented by the base class CommonCell ) 最终的问题是,当我来自不同的类/文件时,尝试将CellSection<T>引用/ CellSection<T>ICellSection (由基类CommonCell

List<ICellSection> Sections = new List<ICellSection> { section1, section2 };

I get an error that it's not allowed. 我收到不允许的错误消息。

Error message: Argument 1: cannot convert from 'CellSection<TextInput>' to 'ICellSection' 错误消息: Argument 1: cannot convert from 'CellSection<TextInput>' to 'ICellSection'

This one I'm more unsure about as I don't know if it's even possible (can't find examples on it), but the problem 1 and 2 I just don't understand why won't work as they seem pretty straight forward... 我不确定这一点,因为我不知道它是否可能(无法找到示例),但是问题1和2我只是不明白为什么它们不起作用,因为它们看起来很简单向前...

Every customer support employee ever: 每位客户支持员工:

"Have you tried turning it off and on again?" “你试过把它关掉再打开吗?”

Me: 我:

"Dam'it... Yes, yes I have turned it off and on again" “该死...是的,是的,我已将其关闭然后再打开”

Turns out this problem was a Visual Studio or Xamarin bug. 原来,这个问题是Visual Studio或Xamarin错误。 I deleted all relevant files and added them again (copying and pasting the same old code into the new files). 我删除了所有相关文件并再次添加了它们(将相同的旧代码复制并粘贴到新文件中)。 Boom! 繁荣! Everything is now inherited properly. 现在一切都已正确继承。 I have no idea what caused the issue, as I assumed it would be a reference problem in the .projitems file, but there's basically no change to that file (checking in git a few lines switched place but that's it). 我不知道是什么引起了这个问题,因为我认为这是.projitems文件中的参考问题,但是对该文件基本上没有任何更改(在git中检查几行切换位置,仅此而已)。

If anyone else has the same or a similar issue in the future: I had restarted Visual Studio several times while having this problem. 如果将来还有其他人遇到相同或类似的问题:在遇到此问题时,我多次重新启动了Visual Studio。 I had restarted my machine at least once while having this problem. 遇到此问题时,我至少重启了计算机一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM