【问题标题】:How to change font awesome icon with click event?如何使用点击事件更改字体真棒图标?
【发布时间】:2021-03-25 23:20:29
【问题描述】:

我在 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>

【问题讨论】:

  • 您是否尝试使用stopWatch.Source = new FontImageSource() { FontFamily=xx ,Glyph =xx }; 来设置真棒图标。
  • 语法正确,编译后没有错误。但是,图标无法显示。
  • 可能是图标的大小,当我点击按钮时,图标消失了,
  • 可能不正确;我将全文复制到属性“Source”,var SourceGreen = new FontImageSource() { FontFamily = "{x:Static fontawesome:FontAwesomeIcons.ArrowAltCircleDown}", Glyph = "{x:StaticResource FontAwesomeRegular}", };跨度>
  • 你已经交换了 FontFamily 和 Glyph 的值

标签: c# xamarin xamarin.forms font-awesome


【解决方案1】:

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

【讨论】:

  • 我在 App.xaml 中定义了各种字体。如何在“Button_OnPressed”中分配“FontFamily”?
  • System.InvalidCastException:指定的强制转换无效。 " UIApplication.Main(args, null, "AppDelegate"; 我试图弄清楚发生了什么
  • 如果资源没有退出,我会尝试用 FontAwesomeRegular2 替换 FontAwesomeRegular。它无法显示,也没有引发异常。异常发生在 main.cs 中: static void Main(string[] args) { // 如果你想使用与“AppDelegate”不同的 Application Delegate 类 // 你可以在这里指定它。 UIApplication.Main(args, null, "AppDelegate"); }
  • 我不知道为什么 (fontFamily as OnPlatform&lt;string&gt;).Android 或 .iOS 为空,这就是我必须使用 Linq 的原因
  • 感谢您的帮助,尝试应用您的评论,尽快回复您。
猜你喜欢
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-23
  • 2021-12-10
  • 2014-06-09
  • 2018-09-26
  • 2020-11-16
相关资源
最近更新 更多