Forum Xamarin.Forms

Can I pass a class object in a XAML theme tag or make it visible globally somehow?

JKnottJKnott Member ✭✭✭
edited December 2019 in Xamarin.Forms

Hello again!
I think I am asking this mainly to @Jarvan but any ideas or guidance is more than welcome!

I am in the process of finishing up some theming on my app, and am having a problem while trying to theme a SyncFusion DataGrid object.

In my code I implemented the following code to define my color themes for the app

DarkTheme.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary
x:Class="BoomStick.Themes.DarkTheme"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">


<x:Object x:Key="dgStyle" ></x:Object>

<x:String x:Key="icoAdd">ic_lte_plus_26px.png</x:String>
<x:String x:Key="icoDelete">ic_lte_delete_sign_filled_26px.png</x:String>
<x:String x:Key="icoRefresh">ic_lte_refresh_26px.png</x:String>
<x:String x:Key="icoSave">ic_lte_save_26px.png</x:String>
<x:String x:Key="icoCamera">ic_lte_camera_26px.png</x:String>

<Color x:Key="primary-back-title-color">#291010</Color>
<Color x:Key="primary-title-color">#919191</Color>
<Color x:Key="NavigationPrimary">#6F6F6F</Color>
<Color x:Key="AppBackgroundColor">#373739</Color>

....

DarkTheme.cs

[Preserve(AllMembers = true)]
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DarkTheme
{
    /// <summary>
    /// Initializes a new instance of the <see cref="DarkTheme"/> class.
    /// </summary>
    public DarkTheme()
    {
        InitializeComponent();
     // I Was hoping that this would work
        DataGridTheme dgStyle = new DataGridTheme();
    }
}

DataGridTheme.cs

/// <summary>
/// 
/// </summary>
[Preserve(AllMembers = true)]
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DataGridTheme
{
    // Custom style class
    /// <summary>
    /// 
    /// </summary>
    public class DgStyle : DataGridStyle
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DgStyle"/> class.
        /// </summary>
        public DgStyle()
        {
        }

        /// <inheritdoc/>
        public override Color GetHeaderBackgroundColor()
        {
            return Color.FromHex("#393838");
        }

        /// <inheritdoc/>
        public override Color GetHeaderForegroundColor()
        {
            return Color.FromHex("#97A3AE");
        }

 .....

and last but not least the implementation
somepage.xaml

<ContentPage.Resources>
    <ResourceDictionary>
        <local:DataGridTheme x:Key="dgStyle" />
    </ResourceDictionary>
</ContentPage.Resources>

Basically I need to know how to specify a different class DGDark() or DGLight() in the XAML or in the class implementation for the theme, so it can be applied across the app?

Thanks!

Best Answer

  • AdamMeaneyAdamMeaney US ✭✭✭✭✭
    Accepted Answer
    <x:Object x:Key="dgStyle" ></x:Object>
    

    should be

    <localNamespaceAlias:DGDark x:Key="dgStyle" />
    <localNamespaceAlias:DGLight x:Key="dgStyle" />
    

    In each ResourceDictionary, respectively. Usage is to either use an implicit style to set the dgStyle resource on your grid, or manually set it using DynamicResource on the object.

Answers

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭
    Accepted Answer
    <x:Object x:Key="dgStyle" ></x:Object>
    

    should be

    <localNamespaceAlias:DGDark x:Key="dgStyle" />
    <localNamespaceAlias:DGLight x:Key="dgStyle" />
    

    In each ResourceDictionary, respectively. Usage is to either use an implicit style to set the dgStyle resource on your grid, or manually set it using DynamicResource on the object.

  • JKnottJKnott Member ✭✭✭

    Awesome! Thanks! I figured I was close but it was alluding me.

    Cheers!

  • JKnottJKnott Member ✭✭✭
    edited December 2019

    Just as a follow up for anyone that follows this thread, here is how I wound up implementing this one.

    DarkTheme.xaml

      <ResourceDictionary
        x:Class="MyApp.Themes.LightTheme"
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:local="clr-namespace:MyApp.Themes">
    
        <local:DgStyleDark x:Key="dgStyle" />
    
        <x:String x:Key="icoAdd">ic_plus_26px.png</x:String>
    

    Implementation in Suplies.CS

        public Supplies()
            {
                    try
                 {
                        InitializeComponent();
    
                        BindingContext = viewModel = new SupplyViewModel();
    
                //Set the theme for the datagrid. 
                        App.Current.Resources.TryGetValue("dgStyle", out object objTheme);
                        DataGrid.GridStyle = (DataGridStyle)objTheme;
                ...
                    }
                    catch (Exception ex)
                    {
                ...
                }
        }
    

    Thanks for the help!

Sign In or Register to comment.