Xamarin.Forms:使用所有页面扩展CommonToolbarPage类

nfg76nw0  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(145)

我在xamarin表单应用程序开发ToolbarItem.我需要在整个应用程序中有ToolbarItem.为此,我已经创建了CommonToolbarPage类,并与ContentPage扩展在所有页面中使用.但Main.xaml.cs页面是继承与TabbedPage .我如何使用CommonToolbarPage与主页.下面的CommonToolbarPage类代码

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

下面是Mainpage.xaml.cs

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

xaml在这里看看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>

如何在Main.xaml.cs页面中使用CommonToolbarPage

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

cs(第一个选项卡第一页)

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

    }
}

当我点击 * 转到第二页 * 按钮得到下面的输出没有toolbarItem(如果我没有继承所有页面文件循环页面)见下面的截图

v8wbuo2f

v8wbuo2f1#

查看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>

您将拥有一个基本视图模型,它将在构造函数中创建项,而不是静态地设置Items。
希望这对你有帮助。

相关问题