用户选择自己选择的大小后,应用程序中的字体大小会动态变化(Xamarin)

Font size in App change dynamically after user selects sizeof their choosing (Xamarin)

我的应用程序中有一个应用程序设置部分,用户可以在其中选择应用程序文本大小的字体大小。

我想我可以使用 Properties.ContainsKey() 来保存用户选择的字体大小。尽管此更改在应用程序关闭并重新启动后仍然存在,但我希望这些更改立即发生。例如,用户在应用程序中更改字体大小 运行,然后当他们单击以转到不同的选项卡或页面时,字体大小会更改。

至此我已经保存了应用页面中的属性,并将我的应用中我想要更改的所有字体绑定到应用属性。当我关闭和打开我的应用程序时,这些更改是持久的,但就像我说的,当我更改字体大小时,我可以在我的应用程序中漫游并且字体保持不变。唯一认为会立即发生变化的是我与滑块绑定的标签,用户可以在其中增加或减小字体大小。

App.xaml.cs public int H1Font

     private const string HeaderFont = "TitleFont";
            private const string BodyFont = "BodyFont";
            {
                get
                {
                    if (Properties.ContainsKey(HeaderFont))
                    {
                        var value = Properties[HeaderFont].ToString();
                        return Int32.Parse(value);
                    }
                        
                    return 18;
                }
                set
                {
                    Properties[HeaderFont] = value;
                }
            }
            //Font for smaller labels
            public int H2Font
            {
                get
                {
                    if (Properties.ContainsKey(BodyFont))
                    {
                        var value = Properties[BodyFont].ToString();
                        return Int32.Parse(value);
    
                    }
                        
                    return 15;
                }
                set
                {
                    Properties[BodyFont] = value;
                }
            }

Page1.xaml

<ListView x:Name="ItemList"
                        ItemSelected="OpenCategorySelect"
                        HasUnevenRows="true"
                      >
               
                <ListView.ItemTemplate>
                <DataTemplate>
                        <ViewCell>
                        <StackLayout x:Name="theStacklayout" 
                                        Orientation="Horizontal">
                            <Image x:Name="ItemImage" 
                                    Aspect="AspectFit"
                                    Source="{Binding ItemID, Converter={StaticResource converter}}"/>
                            <Label x:Name="ItemName" Text="{Binding sDescription}"  
                                   HorizontalOptions="Center" 
                                    VerticalOptions="Center"
                                    FontSize="{Binding H1Font}"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

AppPrefernacePage.xaml.cs

    public AppPrefernacePage()
            {
                InitializeComponent();
                BindingContext = Application.Current;
            }
    
            void FontSlider_ValueChanged(System.Object sender, Xamarin.Forms.ValueChangedEventArgs e)
            {
                var app = Application.Current as App;
             ...
             ...
             ...
    
                app.H1Font = h1Font;
                app.H2Font = newFont;   
            }
    
            protected override async void OnDisappearing()
            {
                base.OnDisappearing();
                await Application.Current.SavePropertiesAsync();
            }
    }

AppPrefernacePage.xaml

    <ViewCell>
        <StackLayout Orientation="Vertical" Margin="15,10,15,10">
           <Grid>
       ...
       ... 
          <Label Text="Gala Apple" Grid.Row="0"
                 Grid.ColumnSpan="2"
                 FontSize="{Binding Source={x:Reference FontSlider},
                                                      Path=Value,
                                                      StringFormat='{0:N0}'}"/>
                                    <Label Text="This type of Apple is very popular."
                                           Grid.Row="1"
                                           Grid.ColumnSpan="2"
                                           FontSize="{Binding Source={x:Reference fontSlider},
                                                      Path=Value,
                                                      StringFormat='{0:N0}'}"/>
    
                                    <local:StepSlider x:Name="fontSlider"
                                                      Minimum ="15"
                                                      Maximum="30"
                                                      Steps="3"
                                                      Value="{Binding H2Font}"
                                                      Grid.Row="2"
                                                      Margin="15,0,15,0"
                                                      ValueChanged="fontSlider_ValueChanged"
                                                      Grid.ColumnSpan="2"
                                                      />
    
                                    <Label Text="A"
                                           HorizontalOptions="Start"
    
                                           Grid.Row="3"
                                            Margin="15,0,0,0"
                                           Grid.Column="0"
                                           />
                                    <Label Text="A"
                                           HorizontalOptions="EndAndExpand"
                                           FontSize="26"
                                           Grid.Row="3"
                                           Margin="0,0,15,0"
                                           Grid.Column="1"/>
                                </Grid>
                            </StackLayout>
                        </ViewCell>
                        <ViewCell>
                        <StackLayout Orientation="Horizontal" >
                            <Label Text="Adjust General Settings"
                                   FontAttributes="Bold"
                                   FontSize="17"
                                   VerticalOptions="Center"
                                   Margin="15,0,5,5"
                                   Padding="5"/>
                        </StackLayout>
                        </ViewCell>
                            <ViewCell>
                            <StackLayout Orientation="Horizontal" >
                                <Image Source="" />
                                <Label Text="Adjust General Settings"
                                        VerticalTextAlignment="Center"
                                        FontSize="{Binding Source={x:Reference fontSlider},
                                                      Path=Value,
                                                      StringFormat='{0:N0}'}"
                                        HeightRequest="55"/>
                            </StackLayout>
                        </ViewCell>

我还将滑块值与字体首选项值绑定。如图

在小姜的帮助下我想通了。我以前使用过 INotifyPropertyChanged 但从未想过在这里使用它。这解决了一半的问题。另一半是让我的 Listview 中的字体发生变化,因为我已经将列表绑定到 itemSource,所以尽管我认为我的更改没有被实现,但它们确实被实现了。 我最终执行了以下操作以获得解决方案。

            private const string BodyFont = "BodyFont";
            {
                get
                {
                    if (Properties.ContainsKey(HeaderFont))
                    {
                        var value = Properties[HeaderFont].ToString();
                        return Int32.Parse(value);
                    }
                        
                return 18;
            }
            set
            {
                Properties[HeaderFont] = value;
                OnPropertyChanged(nameOf(H1Font));

            }
        }
        //Font for smaller labels
        public int H2Font
        {
            get
            {
                if (Properties.ContainsKey(BodyFont))
                {
                    var value = Properties[BodyFont].ToString();
                    return Int32.Parse(value);

                }
                    
                return 15;
            }
            set
            {
                Properties[BodyFont] = value;
                OnPropertyChanged(nameOf(H2Font));

            }
        }---------------------------------------------- **Page1**---------------------
<ListView x:Name="ItemList"
                        ItemSelected="OpenCategorySelect"
                        HasUnevenRows="true">
 <ListView.ItemTemplate>
                <DataTemplate>
                        <ViewCell>
                        <StackLayout x:Name="theStacklayout" 
                                        Orientation="Horizontal">
                            <Image x:Name="ItemImage" 
                                    Aspect="AspectFit"
                                    Source="{Binding ItemID, Converter={StaticResource converter}}"/>
                            <Label x:Name="ItemName" Text="{Binding sDescription}"  
                                   HorizontalOptions="Center" 
                                    VerticalOptions="Center"
                                    FontSize="{Binding BindingContext.H1Font, Source={x:Reference Name=NameOfThisPage}}"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

    **Page1.xaml.cs**
    BindingContext = Application.Current;