There is a page, it connects styles via xaml in this way:

<ContentPage.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <themes:MainStyles /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </ContentPage.Resources> 

The dictionary itself looks like this:

 <ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyMobApp.Themes.MainStyles"> <OnPlatform x:Key="BaseFont" x:TypeArguments="x:String"> <On Platform="Android" Value="cuprum_standard.ttf#Cuprum" /> <On Platform="iOS" Value="Cuprum" /> <On Platform="UWP, WinRT, WinPhone" Value="Assets/Fonts/cuprum_standard.ttf#Cuprum" /> </OnPlatform> <Style x:Key="Grey" TargetType="Label"> <Setter Property="FontFamily" Value="{StaticResource BaseFont}" /> <Setter Property="FontSize" Value="12" /> <Setter Property="TextColor" Value="#A0A0A0" /> </Style> <Style x:Key="h1" TargetType="Label"> <Setter Property="TextColor" Value="#000000" /> <Setter Property="FontSize" Value="14" /> <Setter Property="FontFamily" Value="{StaticResource BaseFont}" /> </Style> 

In XAML, I apply styles like this:

 <Label Text="{Binding Date}" Margin="10,10,10,0" HeightRequest="13" Style="{StaticResource Grey}" /> 

Everything is working. But then it became necessary to add Label to the page dynamically, to which you need to stick the Gray style, for example. Googled and found such a logical option:

 this.Wrapper.Children.Add(new Label { Text = this._Event.Date, Style = Application.Current.Resources["Grey"] as Style }); 

BUT, it throws a System.NullReferenceException: Object reference not set to an instance of an object. exception System.NullReferenceException: Object reference not set to an instance of an object.

I bow to the fact that in my case, not just a Resource, but a matter of MergedDictionaries. How, then, to apply a style from C # code?

  • There is a good (at least) Metanite site :) see setting dynamic / static resources in C # code - Alias
  • @Alias ​​Thank you for the tip-off, but I know this resource, I looked, and there is a little something wrong. I need to get access not to a specific single property (such as Color), but to a set of Setters called a style, i.e. to styles in the dictionary. - Leo

1 answer 1

My guess regarding MergedDictionaries turned out to be true. But I did not understand that despite the fact that the dictionary is connected to XAML, working with it from the code it needs to be additionally connected. I did this:

  1. Connect the space where the style dictionary is located using MyMobApp.Themes ;
  2. Initialized the new resource this.Resources = new ResourceDictionary();
  3. Added to it your dictionary as MergedDictionaries Resources.MergedDictionaries.Add(new MainStyles());
  4. When creating a label, I applied a style from my dictionary.

     this.Wrapper.Children.Add(new Label { Text = this._Event.Date, Style = Resources["Grey"] as Style }); 

ADD: If there is no need for dictionaries (use one, for example), we initialize the resource object with just one of our dictionaries like this: this.Resources = new MainStyles(); and further according to the scheme above.