如何使用单击事件更改字体真棒图标?

问题描述

我在 XAML 文件中指定了很棒的字体。问题是如何在单击按钮时更改字体真棒图标,并在再次单击按钮时将其更改回原始。

Xamarin 表单中的 FontAwesome

    <ImageButton x:Name="stopWatch" BackgroundColor="Green" HeightRequest="60" WidthRequest="60"                   
                         VerticalOptions ="End" HorizontalOptions ="Center" Margin="10,25,10,10"
                         CornerRadius="30"
                         Padding="1" Opacity="0.5"
                             Clicked="Button_Onpressed" >
                    <ImageButton.source>
                        <FontimageSource FontFamily="{x:StaticResource FontAwesomeRegular}" 
                             Glyph="{x:Static fontawesome:FontAwesomeIcons.ArrowAltCircleDown}"
                             Color="{AppThemeBinding Dark=Green,Light=White}"/>
                    </ImageButton.source>
                    <ImageButton.IsVisible>
                        <OnPlatform x:TypeArguments="x:Boolean">
                            <On Platform="iOS"
                    Value="true" />
                            <On Platform="Android"
                    Value="false" />
                        </OnPlatform>
                    </ImageButton.IsVisible>
                </ImageButton>

没有字体真棒的点击事件(我无法指定带有真棒字体的图标)

    private void Button_Onpressed(object sender,EventArgs e)
            {
                if (ButtonBackColor == true)
                    stopWatch.source = "connection.png";
                else stopWatch.source = "signal.png";
                ButtonBackColor = !ButtonBackColor;
            }

更新

在 App.xaml 中定义 fontAwesome

<ResourceDictionary>
            <OnPlatform x:TypeArguments="x:String"
                        x:Key="FontAwesomeBrands">
                <On Platform="Android"
                    Value="FontAwesome5Brands.otf#Regular" />
                <On Platform="iOS"
                    Value="FontAwesome5Brands-Regular" />
                <On Platform="UWP"
                    Value="/Assets/FontAwesome5Brands.otf#Font Awesome 5 Brands" />
            </OnPlatform>
            <OnPlatform x:TypeArguments="x:String"
                        x:Key="FontAwesomeSolid">
                <On Platform="Android"
                    Value="FontAwesome5Solid.otf#Regular" />
                <On Platform="iOS"
                    Value="FontAwesome5Free-Solid" />
                <On Platform="UWP"
                    Value="/Assets/FontAwesome5Solid.otf#Font Awesome 5 Free" />
            </OnPlatform>
            <OnPlatform x:TypeArguments="x:String"
                        x:Key="FontAwesomeRegular">
                <On Platform="Android"
                    Value="FontAwesome5Regular.otf#Regular" />
                <On Platform="iOS"
                    Value="FontAwesome5Free-Regular" />
                <On Platform="UWP"
                    Value="/Assets/FontAwesome5Regular.otf#Font Awesome 5 Free" />
            </OnPlatform>
        </ResourceDictionary>

解决方法

对于AppThemeBinding,目前代码似乎不支持Ability to set AppThemeBinding on Styles in code,您可以测试一次Application.Current.UserAppTheme,但它不会是动态的。

using System.Linq;
...

private void Button_OnPressed(object sender,EventArgs e)
{
  Application.Current.Resources.TryGetValue("FontAwesomeRegular",out object fontFamily);

   if (fontFamily == null)
      //this should not occurred,throw an exception

    string fontFamilyString = Device.RuntimePlatform switch
            {
                Device.Android => (fontFamily as OnPlatform<string>).Platforms.Where(x => x.Platform[0].Equals(Device.Android)).Select(x => (string)x.Value).FirstOrDefault(),Device.iOS => (fontFamily as OnPlatform<string>).Platforms.Where(x => x.Platform[0].Equals(Device.iOS)).Select(x => (string)x.Value).FirstOrDefault(),Device.UWP => (fontFamily as OnPlatform<string>).Platforms.Where(x => x.Platform[0].Equals(Device.UWP)).Select(x => (string)x.Value).FirstOrDefault(),_ => string.Empty
            };

 if (ButtonBackColor == true)
 {
     stopWatch.Source = new FontImageSource()
     { 
       FontFamily= fontFamilyString,Glyph = FontAwesomeIcons.ArrowAltCircleDown,Color = Application.Current.UserAppTheme == OSAppTheme.Dark
               ? Color.Green
               : Color.White;
     };
 }
 else
 {
     stopWatch.Source = new FontImageSource()
     { 
       FontFamily= fontFamilyString,Glyph = FontAwesomeIcons.ArrowAltCircleUp,Color = Application.Current.UserAppTheme == OSAppTheme.Dark
               ? Color.Green
               : Color.White;
     };
 }

 ButtonBackColor = !ButtonBackColor;
}