AppThemeBinding with StaticResource in Codebehind

Jassim Al Rahma 1,596 Reputation points
2023-01-05T22:50:44.99+00:00

Hi,

How can I set the below TextColor in Codebehind?

TextColor="{AppThemeBinding Light={StaticResource FontIconLight}, Dark={StaticResource FontIconDark}}"  

Thanks,
Jassim

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,925 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 79,781 Reputation points Microsoft Vendor
    2023-01-06T03:00:23.457+00:00

    Hello,

    How can I set the below TextColor in Codebehind?

    You can use SetAppThemeColor to set the text color in the code behind.

    For example, I have button and need to set the text color in the code behind.

    • Method 1: If you put your FontIconLight Color in the current page's <ContentPage.Resources>.
      <ContentPage.Resources\>  
      <Color x:Key="FontIconLight"\>red\</Color\>  
      <Color x:Key="LightSecondaryColor"\>Black\</Color\>  
      <Color x:Key="FontIconDark"\>Teal\</Color\>  
      <Color x:Key="DarkSecondaryColor"\>White\</Color\>  
      </ContentPage.Resources\>
    

    You can use this.Resources get the color with following code.

       MyButton.SetAppThemeColor(Button.TextColorProperty, this.Resources["FontIconLight"] as Color, this.Resources["FontIconDark"] as Color);  
    
    • Method 2: If you put your FontIconLight Color in the Resources/Styles/Colors.xaml, you need to create a custom static dictionary to access merged dictionaries in App.xaml.cs like following code.
      public App()  
      {  
      InitializeComponent();  
      MainPage = new AppShell()  
      ResourceDictionary = new Dictionary\<string, ResourceDictionary\>();  
      foreach (var dictionary in Application.Current.Resources.MergedDictionaries)  
      {  
      string key = dictionary.Source.OriginalString.Split(';').First().Split('/').Last().Split('.').First();  
      ResourceDictionary.Add(key, dictionary);  
      }  
      }  
      public static Dictionary\<string, ResourceDictionary\> ResourceDictionary;
    

    Then you can use App.ResourceDictionary get the color with following code.

       MyButton.SetAppThemeColor(Button.TextColorProperty, (Color)App.ResourceDictionary["Colors"]["FontIconLight"], (Color)App.ResourceDictionary["Colors"]["FontIconDark"]);  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

  2. Justin Burcaw 5 Reputation points
    2024-07-26T04:56:52.58+00:00

    I want to provide a slightly better solution than the previous answer; however, I couldn't have done it without their answer first.

    1. Go to App.xaml file and set up your resource dictionaries.
    2. Go to App.cs and add a public static ResourceDictionary MyColors.
    3. Simple search for the name of the file with a linq query and save it to the static variable.
    4. You can now access it from anywhere with your code with App.MyColors["key"].

    App.xaml

    <Application.Resources>
    	<ResourceDictionary>
        	<ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
                <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
     
                <ResourceDictionary Source="Resources/Styles/MyColors.xaml" />
            	<ResourceDictionary Source="Resources/Styles/MyStyles.xaml" />
        	</ResourceDictionary.MergedDictionaries>
    	</ResourceDictionary>
    </Application.Resources>
    

    App.cs

    #pragma warning disable CS8618
    public static ResourceDictionary MyColors = new();
    #pragma warning restore CS8618
     
    public App()
    {
    	InitializeComponent();
    	MyColors = Resources.MergedDictionaries
    		.Where(x => x.Source.ToString().Contains("MyColors"))
    		.First();
    		 
    	MainPage = new AppShell();
    }
    

    Wherever in your application

    Button.BackgroundColor = App.MyColors["Primary"];
    
    0 comments No comments

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.