特定设备的不同字体大小 [英] Different font size for specific device

查看:33
本文介绍了特定设备的不同字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发通用应用程序,我需要分别处理移动和桌面的文本框字体大小.我找到了一些方法,但没有一个方法无法解决问题:以 VisualStateManager 和 StateTrigger 为例:

I am currently developing universal app and I need to handle textbox font size for mobile and desktop separately. I found some approaches but none of them can't handle the problem: Using VisualStateManager with StateTrigger as example:

 <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="ChangeFontSize">
            <VisualState x:Name="Desktop">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="500"/>
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="textBox.FontSize" Value="18" />
                </VisualState.Setters>
            </VisualState>

            <VisualState x:Name="Mobile">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0"/>
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="textBox.FontSize" Value="22" />
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

不适合,因为 StateTrigger 仅在调整屏幕大小时触发.重新定义 xaml 样式:

does not fit because StateTrigger fires only when screen is resizing. Redefine xaml style:

<x:Double x:Key="MyFontSize">22</x:Double>

\\\\\\
........
\\\\\\
Application.Current.Resources["MyFontsSettings"] = 18;

对MyFontSize"没有任何影响,它仍然具有值22".

does not have any effect to the "MyFontSize", it still has value '22'.

是否有任何正确的方法可以正确执行此操作,而无需在每个页面和控件上进行设置?我想在样式中设置一次并在任何地方使用.欢迎提出任何建议.

Is there any proper way to do this correctly, without setting it on every page and control? I want set it once in styles and use everywhere. Any suggestions are welcome.

推荐答案

我的问题是我无法在运行时更改样式中定义的字体大小

My problem is that I can't change font sizes defined in styles in the runtime

对于您的要求,您可以参考在 Template 10<中实现的Setting/a>.创建实现 INotifyPropertyChanged 并包含 FontSize 属性

For your requirement, you could refer Setting that implemented in Template 10. Create Setting class that implements INotifyPropertyChanged and contain FontSize property

public class Setting : INotifyPropertyChanged
{
   private double _fontSize = 20;
   public double FontSize
   {
       get { return _fontSize; }
       set { _fontSize = value; OnPropertyChanged(); }
   }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

在应用程序资源字典中创建一个 Setting 实例以在应用程序启动期间初始化设置.

Make a Setting instance in your application resources dictionary to init setting during app start.

<Application.Resources>
    <ResourceDictionary>
        <local:Setting x:Key="Setting"/>
    </ResourceDictionary>
</Application.Resources>

使用数据绑定将 FontSize 属性绑定到您的 TextBlocks,如下所示.

Use data binding to bind the FontSize property to your TextBlocks like the follow.

<TextBlock Name="MyTextBlock" Text="Hi This is nico" FontSize="{Binding FontSize, Source={StaticResource Setting} }"/>

在运行时更改样式中定义的字体大小.

Change font sizes defined in styles in the runtime.

((Setting)Application.Current.Resources["Setting"]).FontSize = 50;

这篇关于特定设备的不同字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆