Xamarin forms control templates and Theming not picking template colors

Munasinghe, Charith 1 Reputation point
2021-02-01T05:11:47.37+00:00

I have implemented theming functionality in Xamarin application it is working fine in application.
I have implemented some control templates for common buttons used in application. But theming functionality is not working with only for controlled templates. It does not pick any of themes I have created.
For simplicity I have taken Xamarin theme official documentation source code and add controlled template but but I'm facing the same issue which is not applying theme for custom templates.

----------

My controlled Template xaml file

<Frame xmlns="http://xamarin.com/schemas/2014/forms"  
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
             x:Class="ThemingDemo.ControlTemplate.MenuButtonView" x:Name="RootElement"  
      Padding="0" HasShadow="True">  
    <Frame.Resources>  
        <ControlTemplate x:Key="CardViewControlssTemplate">  
            <Frame  BorderColor="{TemplateBinding BindingContext.ShortcutAreaColor}" CornerRadius="{Binding ButtonCornerRadius}" Padding="0" HasShadow="True" BackgroundColor="{TemplateBinding BindingContext.ShortcutAreaColor}">  
                <Grid RowSpacing="0" ColumnSpacing="0">  
                    <Grid.RowDefinitions>  
                        <RowDefinition Height="1*" />  
                        <RowDefinition Height="1*" />  
                    </Grid.RowDefinitions>  
                    <Frame BackgroundColor="{Binding ShortcutAreaColor}" HorizontalOptions="Start" VerticalOptions="Start" CornerRadius="{Binding ShortcutAreaCornerRadius}" Padding="5" IsClippedToBounds="True">  
                        <Label Text="{Binding ShortcutText}"  />  
                    </Frame>  
                    <Image Source="{Binding ButtonImage}"  Margin="0,0,0,0" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Grid.Row="0"  Aspect="AspectFit"  />  
                    <Label Text="{Binding ButtonTitle}" HorizontalTextAlignment="Center" VerticalTextAlignment="Start"  TextColor="#2a78c2" FontFamily="Lato-Bold" FontSize="15" Grid.Row="1" />  
                </Grid>  
            </Frame>  
        </ControlTemplate>  
    </Frame.Resources>  
</Frame>  

Control template code behind file

public partial class MenuButtonView : Frame  
    {  
        public MenuButtonView()  
        {  
            InitializeComponent();  
        }  
  
        #region Bindable Properties for the custom controler template  
        public static readonly BindableProperty ButtonBorderColorProperty = BindableProperty.Create(nameof(ButtonBorderColor), typeof(string), typeof(MenuButtonView), string.Empty);  
        public static readonly BindableProperty ButtonCornerRadiusProperty = BindableProperty.Create(nameof(ButtonCornerRadius), typeof(int), typeof(MenuButtonView), 5);  
  
        public static readonly BindableProperty ShortcutAreaColorProperty = BindableProperty.Create(nameof(ShortcutAreaColor), typeof(string), typeof(MenuButtonView), string.Empty);  
        public static readonly BindableProperty ShortcutAreaCornerRadiusProperty = BindableProperty.Create(nameof(ShortcutAreaCornerRadius), typeof(int), typeof(MenuButtonView), 5);  
  
        public static readonly BindableProperty ShortcutTextProperty = BindableProperty.Create(nameof(ShortcutText), typeof(string), typeof(MenuButtonView), string.Empty);  
        public static readonly BindableProperty ButtonImageProperty = BindableProperty.Create(nameof(ButtonImage), typeof(string), typeof(MenuButtonView), string.Empty);  
        public static readonly BindableProperty ButtonTitleProperty = BindableProperty.Create(nameof(ButtonTitle), typeof(string), typeof(MenuButtonView), string.Empty);  
        #endregion  
  
        #region Properties  
        public string ButtonBorderColor  
        {  
            get => (string)GetValue(ButtonBorderColorProperty);  
            set => SetValue(ButtonBorderColorProperty, value);  
        }  
  
        public int ButtonCornerRadius  
        {  
            get => (int)GetValue(ButtonCornerRadiusProperty);  
            set => SetValue(ButtonCornerRadiusProperty, value);  
        }  
  
        public string ShortcutAreaColor  
        {  
            get => (string)GetValue(ShortcutAreaColorProperty);  
            set => SetValue(ShortcutAreaColorProperty, value);  
        }  
  
        public int ShortcutAreaCornerRadius  
        {  
            get => (int)GetValue(ShortcutAreaCornerRadiusProperty);  
            set => SetValue(ShortcutAreaCornerRadiusProperty, value);  
        }  
        public string ShortcutText  
        {  
            get => (string)GetValue(ShortcutTextProperty);  
            set => SetValue(ShortcutTextProperty, value);  
        }  
        public string ButtonImage  
        {  
            get => (string)GetValue(ButtonImageProperty);  
            set => SetValue(ButtonImageProperty, value);  
        }  
        public string ButtonTitle  
        {  
            get => (string)GetValue(ButtonTitleProperty);  
            set => SetValue(ButtonTitleProperty, value);  
        }  
        #endregion  
    }  

Using control template from another xaml file

   <StackLayout >  
        <controltemplate:MenuButtonView Grid.Row="0" Grid.Column="0"   ButtonBorderColor="{DynamicResource TertiaryTextColor}"  ShortcutAreaColor="{DynamicResource TertiaryTextColor}" ShortcutText="1"  ButtonTitle="dfsdfd" ControlTemplate="{StaticResource CardViewControlssTemplate}" >  
            <controltemplate:MenuButtonView.GestureRecognizers>  
                <TapGestureRecognizer  Command="{Binding test}" />  
            </controltemplate:MenuButtonView.GestureRecognizers>  
        </controltemplate:MenuButtonView>  
    </StackLayout>  

 

But it is not picking any DynamicResource colors. I have defined colors in each theme

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,366 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.