我目前正在开发通用应用程序,我需要分别处理移动设备和桌面的文本框字体大小。
我找到了一些方法,但都不能解决问题:
使用 VisualStateManager 和 StateTrigger 为例:
<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;
对“MyFontSize”没有任何影响,它仍然具有值“22”。
有没有正确的方法可以正确执行此操作,而无需在每个页面和控件上设置它?我想在样式中设置一次并在任何地方使用。欢迎任何建议。
我的问题是我无法更改运行时样式中定义的字体大小
对于您的要求,您可以参考Setting
实施于模板10 https://github.com/Windows-XAML/Template10。
创建实现的设置类INotifyPropertyChanged
并包含FontSize
财产
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));
}
}
Make a 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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)