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

C# 是否可以动态更改工具栏项的图标?,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我想更改选项卡页面工具栏项目中的几个图标,我已经查看了文档,我要么没有抓住要点,要么我希望实现的目标不可行 我目前正在使用FontAwesomeIcons在我的应用程序中填充图标,从静态角度看,它们工作得很好。但在某些情况下,我可能希望更改图标或其颜色或图标包(例如,从浅到实) App.xaml-我使用它来引用我的.otf文件 ExamplePage.xaml-这将是一个当前显示图标的页面(不是动态显示) 因此,此时的代码可以完美地显示一个静态图标,下面是我的尝试,失败了-但没有抛出错误

我想更改选项卡页面工具栏项目中的几个图标,我已经查看了文档,我要么没有抓住要点,要么我希望实现的目标不可行

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

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


ExamplePage.xaml-这将是一个当前显示图标的页面(不是动态显示)


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

ExamplePage.xaml


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

Resources[“FontAwesomeIconPack”]=App.Current.Resources[“FontAwesomeProLight”];
假设资源[“FontAwesomeIconPack”]链接到页面资源字典,而App.Current.Resources[“FontAwesomeProLight”]链接到App.xaml页面,我的假设正确吗


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

如果您想在运行时更改选项卡图标的样式(例如图标、fontSize)。您应该使用自定义渲染器

在iOS中
使用系统;
使用UIKit;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.iOS;
使用xxx;
使用xxx.iOS;
[程序集:ExportRenderer(typeof(TabbedPage)、typeof(MyTabbedPageRenderer))]
命名空间xxx.iOS
{
公共类MyTabbedPageRenderer:TabbedRenderer
{
受保护的覆盖无效OnElementChanged(VisualElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
}
公共覆盖无效视图显示(布尔动画)
{
基本视图显示(动画);
MessagingCenter.Subscribe(此“更改样式”,(参数,位置)=>{
UpdateItem(TabBar.Items[位置],“xxx.png”,“xxx2.png”);
});
}
void UpdateItem(uitabaritem项、字符串图标、字符串选择图标)
{
如果(项==null)
{
返回;
}
//设置默认图标
if(项目?.Image?.AccessibilityIdentifier==图标)
返回;
item.Image=UIImage.FromBundle(图标);
item.Image.AccessibilityIdentifier=图标;
//设置选择图标
如果(项目?.SelecteImage?.AccessibilityIdentifier==选择图标)
返回;
item.SelectedImage=UIImage.FromBundle(selectIcon);
item.SelectedImage.AccessibilityIdentifier=选择图标;
//设置字体
item.SetTitleTextAttributes(新UITextAttributes(){Font=UIFont.SystemFontOfSize(10,UIFontWeight.Light)},UIControlState.Normal);
item.SetTitleTextAttributes(新UITextAttributes(){Font=UIFont.SystemFontOfSize(10,UIFontWeight.Light)},UIControlState.Selected);
}
}
}
在Android中 您可以检查这一点,它通过使用自定义渲染器在Android中提供解决方案

以形式 使用MessagingCenter在需要时发送消息(如单击按钮)

MessagingCenter.Send(此“ChangeStyle”,0);

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


这是在Android和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);
        }
    }
}
MessagingCenter.Send<Object, int>(this, "ChangeStyle", 0);