Different font size for specific device

后端 未结 1 1545
醉话见心
醉话见心 2021-01-29 05:17

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 prob

相关标签:
1条回答
  • 2021-01-29 05:56

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

    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));
        }
    }
    

    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>
    

    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;
    
    0 讨论(0)
提交回复
热议问题