How to Scale textblock fontsize in Winui3 application according to screen resolution?

Sachi 221 Reputation points
2022-03-09T05:16:39.717+00:00

Hi
i am creating sample Winui3 packaged application in c# i need textblock fontsize dynamically calculate with screen resulation please suggest me. the below piece of code working fine for WPF but not Winui3 is there any alternative to this?

private void Window_Loaded(object sender, RoutedEventArgs e)
{
double controlsize = ((SystemParameters.PrimaryScreenWidth / 12) / 3 * 2) / 5 * 0.7;
System.Windows.Application.Current.Resources.Remove("ControlFontSize");
System.Windows.Application.Current.Resources.Add("ControlFontSize", controlsize);
}
Step3 : In the Xaml page use FontSize="{DynamicResource ControlFontSize}" for the control like:

<TextBlock Text="Button" HorizontalAlignment="Left" FontSize="{DynamicResource ControlFontSize}"
Margin="239,163,0,0" VerticalAlignment="Top" Width="75"/>

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
752 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
790 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 83,206 Reputation points
    2022-03-09T07:59:34.213+00:00

    There is no DynamicResource in WinUI

    You can just change the font size by code by naming the TextBlock

    <TextBlock x:Name="TextBlock1"

    then

    TextBlock1.FontSize = controlsize;


0 additional answers

Sort by: Most helpful