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

How to change font awesome icon with click event?

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

Xamarin Forms 中的 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>

Clicked Event without font awesome(我无法指定字体很棒的图标)

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