question

Stesvis-5434 avatar image
0 Votes"
Stesvis-5434 asked Stesvis-5434 commented

Dynamically change App font size in style

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?

dotnet-xamarinforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered Stesvis-5434 commented

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.

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks @KyleWang-MSFT that's exactly what I needed and I totally forgot about it.

It works as intended, however, about 20% of the time I get this exception on save:

System.InvalidOperationException: Collection was modified; enumeration operation may not execute

Then if i save again right away, it works and there is no exception. What could be wrong??

0 Votes 0 ·

@Stesvis-5434 Everything works fine in my test. Did you modify any collection, such as List, in your project? It seems like an issue about Collection.

0 Votes 0 ·

No I am just saving the Resource with the new value. I think that's what the error is referring to, as I am modifying that Dictionary...

0 Votes 0 ·
Show more comments