C# 如何通过单击事件更改字体图标?

C# 如何通过单击事件更改字体图标?,c#,xamarin,xamarin.forms,font-awesome,C#,Xamarin,Xamarin.forms,Font Awesome,我在XAML文件中指定了字体。问题在于,如何在单击按钮时更改字体图标,并在再次单击按钮时将其更改回原始状态 Xamarin形式的FontAwesome 没有字体awesome的单击事件(我无法指定带有字体awesome的图标) 更新 在App.xaml中定义 <ResourceDictionary> <OnPlatform x:TypeArguments="x:String" x:K

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

Xamarin形式的FontAwesome


没有字体awesome的单击事件(我无法指定带有字体awesome的图标)

更新

在App.xaml中定义

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

对于
应用主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题主题

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;
}
使用System.Linq;
...
已按下私有无效按钮(对象发送方,事件参数e)
{
Application.Current.Resources.TryGetValue(“FontAwesomeRegular”,out对象fontFamily);
如果(fontFamily==null)
//不应发生这种情况,请引发异常
string fontFamilyString=Device.RuntimePlatform开关
{
Device.Android=>(fontFamily as on Platform).Platforms.Where(x=>x.Platform[0].Equals(Device.Android))。选择(x=>(string)x.Value.FirstOrDefault(),
Device.iOS=>(fontFamily作为OnPlatform).Platforms.Where(x=>x.Platform[0]。Equals(Device.iOS))。选择(x=>(字符串)x.Value)。FirstOrDefault(),
Device.UWP=>(fontFamily作为OnPlatform).Platforms.Where(x=>x.Platform[0]。Equals(Device.UWP))。选择(x=>(string)x.Value.FirstOrDefault(),
_=>字符串。空
};
如果(ButtonBackColor==true)
{
stopWatch.Source=新FontImageSource()
{ 
FontFamily=fontFamilyString,
Glyph=FontAwesomeIcons.ArrowAltCircleDown,
Color=Application.Current.UserAppTheme==OSAppTheme.Dark
?颜色。绿色
:颜色。白色;
};
}
其他的
{
stopWatch.Source=新FontImageSource()
{ 
FontFamily=fontFamilyString,
Glyph=FontAwesomeIcons.ArrowAltCircleUp,
Color=Application.Current.UserAppTheme==OSAppTheme.Dark
?颜色。绿色
:颜色。白色;
};
}
ButtonBackColor=!ButtonBackColor;
}

您是否尝试使用stopWatch.Source=new FontImageSource(){FontFamily=xx,Glyph=xx}
设置可怕的图标。语法正确,编译后没有错误。但图标无法显示,可能是图标大小,当我点击按钮时,图标消失,可能不正确;我将全文复制到属性“Source”,var SourceGreen=new fontmagesource(){fontfamine=“{x:Static fontawesome:FontAwesomeIcons.ArrowAltCircleDown}”,Glyph=“{x:StaticResource FontAwesomeRegular},};您已经在App.xaml中交换了FontFamily和GlyphI定义的各种字体的值。如何在“按钮按下”中指定“FontFamily”?System.InvalidCastException:指定的强制转换无效。“UIApplication.Main(args,null,“AppDelegate”;我试图弄清楚如果资源不退出,我会尝试用FontAwesomeRegular2替换FontAwesomeRegular2。它无法显示,也没有引发异常。异常发生在Main.cs:static void Main(字符串[]args)中{//如果您想使用与“AppDelegate”//不同的应用程序委托类,可以在这里指定它。UIApplication.Main(args,null,“AppDelegate”);}我不知道为什么
(fontFamily作为平台).Android
或.iOS为空,这就是为什么我要使用LinqThanks寻求您的帮助,请尝试应用您的评论,稍后再与您联系。
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;
}