I'm having issues with inheritance / polymorphism in C # / Xamarin.Forms. There are a few things that I think I will be allowed to do, but I'm not, and from what I collect, it seems that I am not inheriting correctly, so first first:
ads
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; }
}
public CellSection ()
:base()
{
}
public override IInputType GetInputType()
{
return InputType;
}
}
I have included only one property and one method to demonstrate my problem, but there are many others stated in the same way ...
Problem 1
What happens when I create a new CellSection, for example:
CellSection<TextInput> section1 = new CellSection<TextInput>();
section1.Title = "New Title"; //This line is not allowed
I am not allowed to access the Title property ...
Error message: CellSection<TextInput>' does not contain a definition for 'Title'
I managed to access it by adding the following code to the class CellSection<T>:
public string Title
{
get { return base.Title; }
set { base.Title = value; }
}
but it seems so damn unnecessary and redundant ... There must be another way ...
2
, GetInputType(), , , :
public override IInputType GetInputType()
{
return InputType;
}
: 'CellSection<T>.GetInputType()': no suitable method found to override
3
, , /, / CellSection<T> ICellSection ( CommonCell)
List<ICellSection> Sections = new List<ICellSection> { section1, section2 };
, .
: Argument 1: cannot convert from 'CellSection<TextInput>' to 'ICellSection'
, , ( ), 1 2 , , ...