c# - 是否可以动态更改 ToolbarItem 的图标?

标签 c# xaml xamarin xamarin.forms

我想更改位于 TabbedPage 的 ToolbarItem 中的几个图标,我查看了文档 here ,我要么没有捕获要点,要么我希望实现的目标是不可行的?

我目前正在使用 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>

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

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

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

ExamplePage.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 页面是否正确?

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

最佳答案

如果你想在运行时改变标签图标的样式(比如图标,字体大小)。你应该使用Custom Renderer

在 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);
        }
    }
}

在安卓中

你可以查看这个blog ,它通过使用 Custom Renderer 在 Android 中提供解决方案。

在表单中

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

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

关于c# - 是否可以动态更改 ToolbarItem 的图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58273502/

相关文章:

c# - 如何使用 Regex.Replace 从字符串中删除数字?

c# - 面板 ArrangeOverride 和 MeasureOverride 方法

c# - 将 DataContext 窗口绑定(bind)到 ViewModel

android - Xamarin Android 中的 BindableViewPager

android - "CopyAndConvertResources"任务意外失败(只读资源)

c# - 从输入中转义的 SQL 特殊字符

c# - 为什么 TimerTrigger 在基本 WebJobs SDK v3 主机应用程序中不要求存储帐户?

javascript - 是否允许在 Windows Phone 8.1 应用程序的 webview 的 html 文件中包含外部 javascript 文件?

c# - 在 XAML 中以声明方式定义 DataGrid 的行

c# - 使用命名管道时 Xamarin Forms 中的“方法或操作未实现”