Alan2
Alan2

Reputation: 24592

Is there any way I can bring properties into a Class without using Inheritance?

I am using Xamarin which requires that my CS class and XAML inherits from a Xamarin object like this:

CS

namespace Japanese.Templates
{
    public partial class TimeIntervalTemplate : ContentView
    {
        public TimeIntervalTemplate()
        {
            InitializeComponent();
        }

        // All my time templates contain this and I would
        // like to not have to repeat these many times in 
        // each time template
        public static readonly BindableProperty SelectedValProperty =
           BindableProperty.Create(
               "SelectedVal", typeof(string), typeof(CardOrderTemplate),
               defaultBindingMode: BindingMode.TwoWay,
               defaultValue: default(string));

        // All my time templates contain this and I would
        // like to not have to repeat these many times in 
        // each time template
        public string SelectedVal
        {
            get { return (string)GetValue(SelectedValProperty); }
            set { SetValue(SelectedValProperty, value); }
        }

XAML

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:local="clr-namespace:Japanese;assembly=Japanese" 
             xmlns:b="clr-namespace:Behaviors;assembly=Behaviors" 
             x:Class="Japanese.Templates.TimeIntervalTemplate" x:Name="this">
    <StackLayout BackgroundColor="#FFFFFF" Padding="20,0" HeightRequest="49" Margin="0">

But the same properties and objects here are used in many different classes:

What I would like to do is to simply create a BaseTemplate Class that inherits from Content view. Add the properties to that and then have TimeIntervalTemplate inherit from BaseTemplate. However when I do this:

public class BaseTemplate : ContentView
... 

public partial class TimeIntervalTemplate : BaseTemplate
...

Then it tells me I cannot do this as partial classes must inherit from the same base class.

Is there any way around this? Anyway that I could add in the properties such as SelectedValProperty .. without inheritance from a Base class?

Upvotes: 0

Views: 184

Answers (1)

Sharada
Sharada

Reputation: 13601

You are seeing that error simply because the base class type in code-behind is different from the base class type you used in XAML.

Once you make sure that both base class types are same - XAML compiler will be happy.

<?xml version="1.0" encoding="UTF-8"?>
<!-- make sure change root tag from ContentView to base class type -->
<!-- ('jt' represents the tag prefix for xmlms namespace declaration) -->

<jt:BaseTemplate xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:jt="clr-namespace:Japanese.Templates"

    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-namespace:Japanese;assembly=Japanese" 
    xmlns:b="clr-namespace:Behaviors;assembly=Behaviors" 
    x:Class="Japanese.Templates.TimeIntervalTemplate" x:Name="this">

    <!-- your content here -->

</jt:BaseTemplate>

Upvotes: 1

Related Questions