【问题标题】:Is it possible to dynamically change the icon of a ToolbarItem?是否可以动态更改 ToolbarItem 的图标?
【发布时间】:2020-02-04 23:34:28
【问题描述】:

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

我目前正在使用 FontAwesomeIcons 在我的应用程序中填充图标,从静态的角度来看,它们效果很好。但在某些情况下,我可能希望更改图标、颜色或图标包(例如 Light to Solid)。

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 页面链接?

我希望在这个示例中能够显示我现有的图标,但事实并非如此。我的期望是和以前一样的图标(在我换包之前),但我只是得到一个?)。

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案1】:

    如果你想在运行时改变标签图标的样式(如icon,fontSize)。你应该使用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,它通过使用自定义渲染器提供了Android中的解决方案。

    在表单中

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

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

    【讨论】:

    • 我认为这不再适用了。
    【解决方案2】:

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

    这是在 Android 和 iOS 上测试的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 2020-05-22
      相关资源
      最近更新 更多