How to change the size of the font for whole project during runtime?

Valentine 0 Reputation points
2023-03-11T09:16:19.26+00:00

Hello everyone, I want to make a dynamic update of the font size, I tried it like here(https://learn.microsoft.com/en-us/training/modules/use-shared-resources/4-use-and-update-dynamic-resources), but not everything ok, however I used dynamic resources. On SettingsPage it works, on TaskPage it does not. Снимок экрана 2023-03-11 110826

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,023 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 49,611 Reputation points Microsoft External Staff
    2023-03-13T03:17:40.1633333+00:00

    Hello,

    This is because when a Resource is set, its scope is limited to the current page.

    You can save the resource key to the Preferences class in MAUI and load the dynamic resource in the OnDisplaying method of the TaskPage.

    Please refer to the following documentation and code:

    Write Preferences.Default.Set("FontSizeKey", "Your_Resource_Key"); to the method that changes the fontsize.

    Read dynamic resources when the taskpage loads:

    protected override void OnAppearing()
    {
        base.OnAppearing();
        var size = Preferences.Default.Get("FontSizeKey", "Unknown");
        if (!size.Equals("Unknown"))
        {
            Resources["FS"] = App.Current.Resources[size];
        }
    }
    
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.