【发布时间】: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