1

I am developing ToolbarItem in xamarin forms app. I need to have ToolbarItem throughout the app. For doing so I have created CommonToolbarPage class and extending with ContentPage to use in all pages. But Main.xaml.cs page is inherites with TabbedPage. How can I use CommonToolbarPage with main page. Below CommonToolbarPage class code

public class CommonToolbarPage : ContentPage    
{
    public CommonToolbarPage()
        : base()
    {
        Init();
    }
    private void Init()
    {
        this.ToolbarItems.Add(new ToolbarItem() { Icon="kid", Priority = 0, Order = ToolbarItemOrder.Primary });
        this.ToolbarItems.Add(new ToolbarItem() { Text = "License", Priority = 0, Order = ToolbarItemOrder.Primary });           
    }
}

And below is Mainpage.xaml.cs class

 public partial class MainPage : TabbedPage
        {
            public MainPage()
            {
                InitializeComponent();
            }
        }

Mainpage.xaml here look at Mykid

    <?xml version="1.0" encoding="utf-8" ?>
<CustomPages:CommonToolbarPage2  xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TabbedApp.MainPage"
             xmlns:CustomPages="clr-namespace:TabbedApp.CustomPages;assembly=TabbedApp"   
             xmlns:local="clr-namespace:TabbedApp">
    <local:DairyTabs Icon="dairy" HeightRequest="10" WidthRequest="10" ></local:DairyTabs>
    <local:Mykid Icon="kid" ></local:Mykid>
    <local:Event Icon="about"></local:Event>
</CustomPages:CommonToolbarPage2>

How can I use CommonToolbarPage with Main.xaml.cs page

public partial class MainPage : CommonToolbarPage2
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }

DiaryTab.xaml.cs(First tab first page)

namespace TabbedApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class DairyTabs : ContentPage
    {
        public DairyTabs()
        {
            InitializeComponent();
            btnDemo.Clicked += delegate
            {
                CreateTabs();      
            };
        }
        private async void CreateTabs()
        {
            TabbedPage tp = new TabbedPage();
            tp.Children.Add(new ChildPage());
            tp.Children.Add(new Mykid());
            tp.Children.Add(new Event());          
            await Navigation.PushAsync(tp,false);   
     }

    }
}

When I am clicking on Go for 2nd page button getting below output without toolbarItem(If I am not inheriting all page file cycle pages) See below screenshot

enter image description here

4

1 回答 1

1

在 Xamarin 论坛上查看此主题:https ://forums.xamarin.com/discussion/97340/create-toolbaritems-using-template-or-some-other-dynamic-mechanism

您的代码似乎完全合法。但是在寻找渲染器TabbedPage时,您无法继承ContentPage并将其应用于 TabbedPage`。您应该为此类页面创建一个新的基类:

public class CommonToolbarPage : TabbedPage
    {
        public CommonToolbarPage()
            : base()
        {
            Init();
        }

        private void Init()
        {
            this.ToolbarItems.Add(new ToolbarItem() { Text = "Help", Priority = 0, Order = ToolbarItemOrder.Secondary});
            this.ToolbarItems.Add(new ToolbarItem() { Text = "License", Priority = 0, Order = ToolbarItemOrder.Secondary });
            this.ToolbarItems.Add(new ToolbarItem() { Text = "About", Priority = 0, Order = ToolbarItemOrder.Secondary });
        }

    }    

如果您使用的是MVVM而不是CodeBehing开发方法,则可以使用 Toolbar Item 属性,XAML如下所示:

<ContentPage.ToolbarItems>
    <ToolbarItem Name="MenuItem1" Order="Primary" Icon="Microsoft.png" Text="Item 1" Priority="0" />
    <ToolbarItem Name="MenuItem2" Order="Primary" Icon="Xamarin.png" Text="Item 2" Priority="1" />
</ContentPage.ToolbarItems>

您将拥有一个在构造函数中创建项目的基本视图模型,而不是静态设置项目。

希望这会帮助你。

于 2018-03-21T09:03:24.910 回答