font size scaling in Windows Store Universal App (W8.1 + WP8.1)

前端 未结 5 1629
深忆病人
深忆病人 2020-12-24 03:03

How do i scale text in Windows Store Universal App (W8.1 + WP8.1)? Basically, the app should look the same regardless which device/resolution is used. The current situation

5条回答
  •  一生所求
    2020-12-24 03:22

    I found that using the IValueConverter method worked pretty well when used in conjunction with the ResolutionScale property:

    class ScaleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var resolutionScale = (int)DisplayInformation.GetForCurrentView().ResolutionScale / 100.0;
            var baseValue = int.Parse(parameter as string);
            var scaledValue = baseValue * resolutionScale;
            if (targetType == typeof(GridLength))
                return new GridLength(scaledValue);
            return scaledValue;
        }
    }
    

    Please note, that if you use for more than FontSize (as I am also using it for ColumnDefinition.Width) you will need to handle returning the proper Type'ed output.

    My XAML usage looks something like this:

    
    

    And to just complete the solution, for each XAML page you use this on, you need to include the following, replacing SwapChainBackgroundPanel with whatever class you are using, and defining the xml namespace properly:

    
        
    
    

提交回复
热议问题