Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Creating and Consuming resources in C#

I am tansitioning to C# from XAML. I converter the xaml code in the app to C#. But stuck in using resources. The official documentation doesn't have any thing related to resources.
Specifically I want to use AppThemeBinding for the darkmode and lightmode colors in my C# content Page. As I am transitioning to C# I will only have App.cs file. So, I also want to replace previous XAML code where App Resources are defined with C#.

Best Answer

  • ColeXColeX Member, Xamarin Team Xamurai
    edited October 5 Accepted Answer

    First create the style in code , something like

            Resources = new ResourceDictionary();
            Resources.Add("LightPrimaryColor", Color.Red);
            Resources.Add("DarkPrimaryColor", Color.Green);
    

    Then consume the Resources

            Label label = new Label();
            label.Text = "111";
            label.SetAppThemeColor(Label.TextColorProperty, (Color)Resources["LightPrimaryColor"], (Color)Resources["DarkPrimaryColor"]);
    
            Content = label;
    

Answers

  • stXamDevstXamDev Member ✭✭✭

    @ParamjitSingh Did not quite got your point . Can you elaborate .

  • ColeXColeX Member, Xamarin Team Xamurai
    edited October 5 Accepted Answer

    First create the style in code , something like

            Resources = new ResourceDictionary();
            Resources.Add("LightPrimaryColor", Color.Red);
            Resources.Add("DarkPrimaryColor", Color.Green);
    

    Then consume the Resources

            Label label = new Label();
            label.Text = "111";
            label.SetAppThemeColor(Label.TextColorProperty, (Color)Resources["LightPrimaryColor"], (Color)Resources["DarkPrimaryColor"]);
    
            Content = label;
    
Sign In or Register to comment.