是否可以动态更改工具栏项的图标?



我想更改位于选项卡式页面工具栏项中的几个图标,我在这里查看了文档,但我要么错过了重点,要么我希望实现的目标不可行?

我目前正在使用FontAwesomeIcons在我的应用程序中填充图标,从静态的角度来看,它们工作得很好。但在某些情况下,我可能希望更改图标、颜色或图标包(例如,从浅色到实心(。

App.xaml- 我用它来引用我的.otf文件

<Application.Resources>
<OnPlatform x:Key="FontAwesomeProLight" x:TypeArguments="x:String">
<On Platform="iOS" Value="FontAwesome5Pro-Light" />
</OnPlatform>
<OnPlatform x:Key="FontAwesomeProSolid" x:TypeArguments="x:String">
<On Platform="iOS" Value="FontAwesome5Pro-Solid" />
</OnPlatform>
</Application.Resources>

示例Page.xaml- 这将是一个页面,我当前将显示我的图标(不是动态的(

<TabbedPage.ToolbarItems>
<ToolbarItem Clicked="OnFilterOrders">
<ToolbarItem.IconImageSource>
<FontImageSource FontFamily="{StaticResource FontAwesomeProLight}" Glyph="{x:Static fonts:FontAwesomeIcons.Filter}" />
</ToolbarItem.IconImageSource>
</ToolbarItem>
</TabbedPage.ToolbarItems>

因此,此时的代码可以完美地显示静态图标,下面是我的尝试失败了 - 但没有抛出错误,我只是得到一个 ?

示例页面.xaml

<TabbedPage.ToolbarItems>
<ToolbarItem Clicked="OnFilterOrders">
<ToolbarItem.IconImageSource>
<FontImageSource FontFamily="{DynamicResource FontAwesomeIconPack}" Glyph="{x:Static fonts:FontAwesomeIcons.Filter}" />
</ToolbarItem.IconImageSource>
</ToolbarItem>
</TabbedPage.ToolbarItems>

使用该页面的代码隐藏,我在构造函数中也有以下内容。

Resources["FontAwesomeIconPack"] = App.Current.Resources["FontAwesomeProLight"];
我假设 Resources[">

FontAwesomeIconPack"] 与页面资源字典链接,而 App.Current.Resources["FontAwesomeProLight"] 链接到 app.xaml 页面是否正确?

我希望在这个例子中,我能显示我现有的图标,但它没有。我的期望与以前相同(在我更换包装之前(,但我只是得到一个?

如果要在运行时更改选项卡图标的样式(例如图标,字体大小(。您应该使用自定义渲染器

在 iOS 中

using System;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using xxx;
using xxx.iOS;
[assembly:ExportRenderer(typeof(TabbedPage),typeof(MyTabbedPageRenderer))]
namespace xxx.iOS
{
public class MyTabbedPageRenderer:TabbedRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
MessagingCenter.Subscribe<Object, int>(this, "ChangeStyle", (args, position) => {
UpdateItem(TabBar.Items[position], "xxx.png","xxx2.png");
});
}
void UpdateItem(UITabBarItem item, string icon,string selectIcon)
{
if (item == null)
{
return;
}

// set default icon
if (item?.Image?.AccessibilityIdentifier == icon)
return;
item.Image = UIImage.FromBundle(icon);
item.Image.AccessibilityIdentifier = icon;
//set select icon
if (item?.SelectedImage?.AccessibilityIdentifier == selectIcon)
return;
item.SelectedImage = UIImage.FromBundle(selectIcon);
item.SelectedImage.AccessibilityIdentifier = selectIcon;

//set font
item.SetTitleTextAttributes(new UITextAttributes() { Font=UIFont.SystemFontOfSize(10,UIFontWeight.Light)},UIControlState.Normal);
item.SetTitleTextAttributes(new UITextAttributes() { Font = UIFont.SystemFontOfSize(10, UIFontWeight.Light) }, UIControlState.Selected);
}
}
}

在安卓中

您可以查看此博客,它通过使用自定义渲染器提供了Android中的解决方案。

在表单中

使用"消息中心"在需要时发送邮件(如单击按钮(

MessagingCenter.Send<Object, int>(this, "ChangeStyle", 0);

事情可能已经改变,但我只是将子导航页面的 IconImageSource 绑定到视图模型字符串,动态图标选择工作正常。

这是在Android和iOS上测试的。

最新更新