特定设备的字体大小不同

时间:2018-05-11 11:31:29

标签: c# xaml uwp

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

 <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样式:

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

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

对&#34; MyFontSize&#34;没有任何影响,它仍然有价值&#39; 22&#39;。

有没有正确的方法来正确地执行此操作,而无需在每个页面和控件上进行设置?我希望在样式中设置一次并在任何地方使用。欢迎任何建议。

1 个答案:

答案 0 :(得分:2)

  

我的问题是我无法更改运行时

中样式中定义的字体大小

根据您的要求,您可以参考Template 10中实施的Setting。 创建实现INotifyPropertyChanged并包含FontSize属性

的Setting类
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实例以进行初始化设置。

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

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

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

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

((Setting)Application.Current.Resources["Setting"]).FontSize = 50;
相关问题