簡體   English   中英

無法更改 Xamarin Forms v4.1 中的工具欄圖標

[英]Cannot change Toolbar icon in Xamarin Forms v4.1

當應用程序使用 XF Shell 包裝時,工具欄圖標在運行時不會改變。 示例源: https : //github.com/chyzy/XF-Shell-ToolbarItem-Issue

如果我使用 new NavigationPage(new MainPage) 而不是 Shell,一切正常(圖標更改正確)。

應用程序.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()
                    }
                }
            });
        }
    }

主頁.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>

主頁.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 有問題嗎? 是否可以解決此問題並動態更改圖標?

我的猜測是,一旦將工具欄項添加到渲染器中的本機控件,更改的屬性就不會傳播。

您可以使用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 )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM