Dynamically change App font size in style

Stesvis 1,041 Reputation points
2021-09-24T22:09:53.73+00:00

I have some styles defined in my App.xaml, such as:

<x:Double x:Key="defaultFontSize">14</x:Double>

<Style TargetType="Label">
    <Setter Property="FontSize" Value="{StaticResource defaultFontSize}" />
</Style>

<Style TargetType="Button">
    <Setter Property="FontSize" Value="{StaticResource defaultFontSize}" />
</Style>

<Style TargetType="Entry">
    <Setter Property="FontSize" Value="{StaticResource defaultFontSize}" />
</Style>

<Style TargetType="Editor">
    <Setter Property="FontSize" Value="{StaticResource defaultFontSize}" />
</Style>

<Style TargetType="Picker">
    <Setter Property="FontSize" Value="{StaticResource defaultFontSize}" />
</Style>

<Style TargetType="AnotherControl">
    <Setter Property="FontSize" Value="{StaticResource defaultFontSize}" />
</Style>

Now the users want to be able to increase/decrease the FontSize of every control throughout the app.
So I made a Preferences page where they can pick their own preferred FontSize and I save it in Xamarin.Essentials.Preferences.Set("FontSize", value).

The problem now is, I would like to remove this line from my style:

<x:Double x:Key="defaultFontSize">14</x:Double>

And replace it with some code behind equivalent that reads the value from the Preferences and dynamically sets the defaultFontSize style.

How can I achieve something like that?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Kyle Wang 5,531 Reputation points
    2021-09-27T01:49:08.803+00:00

    Hi Stesvis-5434,

    Welcome to our Microsoft Q&A platform!

    You can use DynamicResource in App.xaml instead, then you can modify the font size dynamically. Here is the documentation you can refer to: Dynamic Styles in Xamarin.Forms.

    The following is a simple demo using DynamicResource.

    App.xaml

    <x:Double x:Key="defaultFontSize">14</x:Double>  
    <Style TargetType="Label">  
        <Setter Property="FontSize" Value="{DynamicResource defaultFontSize}" />  
    </Style>  
    <Style TargetType="Button">  
        <Setter Property="FontSize" Value="{DynamicResource defaultFontSize}" />  
    </Style>  
    <Style TargetType="Entry">  
        <Setter Property="FontSize" Value="{DynamicResource defaultFontSize}" />  
    </Style>  
    

    xaml.cs

    protected override void OnAppearing()  
    {  
        base.OnAppearing();  
        App.Current.Resources["defaultFontSize"] = Preferences.Get("FontSize", 14);  
    }  
      
    private void ButtonSave_Clicked(object sender, EventArgs e)  
    {  
        int value = Convert.ToInt32(fontEnrty.Text);  
        Preferences.Set("FontSize", value);  
        App.Current.Resources["defaultFontSize"] = Preferences.Get("FontSize", 14);  
    }  
    

    Regards,
    Kyle


    If the response is helpful, please click "Accept Answer" and upvote it.

    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.

    1 person found this answer helpful.