Accessing ResourceDictionary from xaml to c# programmatically

KevinGuevaraKevinGuevara USUniversity ✭✭

How do I access the contents of my ResourceDictionary that is located my App.xaml page?

I am trying to make a label's text color that uses a color in my resource dictionary with key = "textColor"

var colorLabel = new Label { Text = "colored Text ", TextColor = (Color) Resources["textColor"]};

Gives me a null error

Best Answer

Answers

  • KevinGuevaraKevinGuevara USUniversity ✭✭

    I have also tried
    Color textColor = (Color) App.Current.Resources["textColor"];

    but still getting errors

  • AnthonyRamirezAnthonyRamirez USUniversity ✭✭✭

    That should work. Can you paste in what your resource definition looks like.

  • KevinGuevaraKevinGuevara USUniversity ✭✭

    <?xml version="1.0" encoding="utf-8" ?> <Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:ProjName;assembly=ProjName" x:Class="ProjName.App"> <Application.Resources> <ResourceDictionary> <x:String x:Key="textColor">#FFB500</x:String> </ResourceDictionary> </Application.Resources> </Application>

    In my XAML code, I would always use {StaticResource textColor} for my color, and it did provide the result.

  • KevinGuevaraKevinGuevara USUniversity ✭✭

    TextColor = Color.FromHex(Application.Current.Resources["textColor"].ToString())

    This worked.

  • ErnestoRojoJrErnestoRojoJr USMember ✭✭

    Cool. Glad my solution worked for you. Yeah, your xaml looks right, too.

  • KevinGuevaraKevinGuevara USUniversity ✭✭

    Another Question.

    I have platform specific Resources that Id like to access on the same label.

    <OnPlatform x:Key="subtitleSize2" x:TypeArguments="x:Double"> <OnPlatform.iOS>13</OnPlatform.iOS> <OnPlatform.Android>13</OnPlatform.Android> <OnPlatform.WinPhone>20</OnPlatform.WinPhone> </OnPlatform>

    This is one of the text sizes that Id like to use on my c sharp code.

    I have tried adding this to my label in my c sharp code

    FontSize = Convert.ToDouble(Application.Current.Resources["subtitleSize2"])

    but I will get an invalid cast error. The type of Application.Current.Resources["subtitleSize2"] when printed out to the debugger is Xamarin.Forms.OnPlatform`1[System.Double]

    I have tried casting the resource to double as well and it will give me the same invalid cast error

  • AnthonyRamirezAnthonyRamirez USUniversity ✭✭✭

    @KevinGuevara

    Regarding Resource Dictionary the following is what I have used for colors

            <Color x:FactoryMethod="FromHex" x:Key="BackgroundColor">
                <x:Arguments>
                    <x:String>1E000000</x:String>
                </x:Arguments>
            </Color>
    

    I imagine you can also use OnPlatform in there as well.

    Hope that helps.

  • VisionsInCodeVisionsInCode USMember ✭✭

    I know its old but regarding the platform specific double issue, you can do it like this:
    Xamarin.Forms.OnPlatform<Xamarin.Forms.Size> mySize= (Xamarin.Forms.OnPlatform<Xamarin.Forms.Size>)Application.Current.Resources["subtitleSize2"]

    FontSize = mySize.iOS;

  • pawanyapawanya INMember ✭✭
    edited February 2018

    In may code ContentPage.Resource is not showing then what i do for this

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:stylesPro"
                 x:Class="stylesPro.MainPage">
      <ContentPage.Resource> //////Resource is not Showing in intelligence
        <Style x:Key="sbtn" TargetType="Button">
          <Setter Property="TextColor" Value="#ff944e"/>
        </Style>
      </ContentPage.Resource>
      <ContentPage.Content>
        <StackLayout Padding="0,20,0,0">
          <StackLayout.Resource>//////Resource is not Showing in intelligence
            <Style x:Key="sbtn" TargetType="Button">
              <Setter Property="TextColor" Value="#ff944e"/>
    
            </Style>
          </StackLayout.Resource>
    
  • EnisNecipogluEnisNecipoglu USMember ✭✭
    edited April 2018

    When I set a DynamicResource in XAML. It triggers when I changed that resource in programmatically and all the colors where I set in XAML changed.
    But all the colors which I set in C# didn't changed and Triggered. Is there any way to set like SetBinding etc. to tiggering when resource changed ?

  • EnisNecipogluEnisNecipoglu USMember ✭✭
    edited April 2018

    @EnisNecipoglu said:
    When I set a DynamicResource in XAML. It triggers when I changed that resource in programmatically and all the colors where I set in XAML changed.
    But all the colors which I set in C# didn't changed and Triggered. Is there any way to set like SetBinding etc. to tiggering when resource changed ?

    I've solved, Maybe you'll need this:

        var lblLabel = new Label { Text = "Hello World!" };
                lblLabel.SetDynamicResource(Label.TextColorProperty, "TextColor");
    

    When you set like that, When the "TextColor" resource changes at runtime, your label textcolor will be updated too,

    SetDynamicResource() is more usable for Dynamic Colors or something else

Sign In or Register to comment.