【问题标题】:Cannot change Toolbar icon in Xamarin Forms v4.1无法更改 Xamarin Forms v4.1 中的工具栏图标
【发布时间】:2019-12-18 07:51:41
【问题描述】:

当应用程序使用 XF Shell 包装时,工具栏图标在运行时不会改变。样本来源:https://github.com/chyzy/XF-Shell-ToolbarItem-Issue

如果我使用 new NavigationPage(new MainPage) 而不是 Shell,一切正常(图标更改正确)。

App.xaml.cs:

public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new AppShell();
        }
    }

AppShell.cs

public partial class AppShell : Shell
    {
        public AppShell ()
        {
            InitializeComponent ();

            this.Items.Add(new ShellSection()
            {
                Title = "Main Page",
                Items =
                {
                    new ShellContent()
                    {
                        Content = new MainPage()
                    }
                }
            });
        }
    }

MainPage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:TISample"
             x:Class="TISample.MainPage"
             Title="Main Page">

    <ContentPage.ToolbarItems>
        <ToolbarItem x:Name="MyToolbarItem" Clicked="MyToolbarItem_OnClicked"/>
    </ContentPage.ToolbarItems>

    <StackLayout>
        <Label Text="Click the toolbar icon to change its color" 
               HorizontalOptions="Center"
               VerticalOptions="Center" />
    </StackLayout>

</ContentPage>

MainPage.xaml.cs

public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            _availableIcons = new[] { "red.png", "green.png" };
            _currentIcon = _availableIcons.First();

            MyToolbarItem.IconImageSource = ImageSource.FromFile(_currentIcon);
        }

        private string[] _availableIcons;
        private string _currentIcon;

        private void MyToolbarItem_OnClicked(object sender, EventArgs e)
        {
            _currentIcon = _availableIcons.First(ai => ai != _currentIcon);
            MyToolbarItem.IconImageSource = ImageSource.FromFile(_currentIcon);
        }
    }

Xamarin Forms Shell 有问题吗?是否可以解决此问题并动态更改图标?

【问题讨论】:

  • 需要更多答案。这是行不通的。无法更改 ToolbarItem 文本和图标。

标签: c# .net xamarin xamarin.forms


【解决方案1】:

我的猜测是,一旦将工具栏项添加到渲染器中的本机控件,更改的属性就不会传播。

您可以使用TitleView 来实现:

<NavigationPage.TitleView>
    <StackLayout Orientation="Horizontal">
        <Image Source="back_button"
               VerticalOptions="CenterAndExpand"
               HorizontalOptions="Start" />
        <Label Text="Page Title"
               HorizontalOptions="StartAndExpand"
               VerticalOptions="CenterAndExpand"
               FontAttributes="Bold"
               FontSize="20"/>
        <ImageButton Source="green.png"
                     VerticalOptions="CenterAndExpand"
                     HorizontalOptions="End" />
    </StackLayout>
</NavigationPage.TitleView>    

https://www.andrewhoefling.com/Blog/Post/xamarin-forms-title-view-a-powerful-navigation-view

编辑:要使用 TabbedPage,首先添加 TitleView,然后添加 TabbedPage Children (https://forums.xamarin.com/discussion/139894/putting-an-image-on-navigation-bar)。

【讨论】:

  • 是否可以使用原生后退按钮图标?我的意思是无需下载,与 Xamarin 在特定平台上提供的相同
  • NavigationPage.TitleView 不适用于 TabbedPage :(
  • 根据这篇文章:forums.xamarin.com/discussion/139894/… 你需要把标题视图定义放在标签页子定义之前
  • 我从 xaml.cs 添加页面(这些页面需要构造函数中的复杂参数)。在 OnAppearing() 中添加页面。你有什么解决办法吗?
  • 对不起,我不明白你的问题。我只能重复一遍:首先添加标题视图,然后添加标签页子项。
猜你喜欢
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 2017-12-28
相关资源
最近更新 更多