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

标签 inheritance xamarin xamarin.forms toolbaritems

我正在 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();
            }
        }

Mainpage.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>

如何将 CommonToolbarPage 与 Main.xaml.cs 页面一起使用

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

DiaryTab.xaml.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的情况下得到以下输出(如果我没有继承所有页面文件循环页面) 请参阅下面的屏幕截图

enter image description here

最佳答案

在 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而不是CodeBehining开发方法,您可以绑定(bind)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>

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

希望这对您有帮助。

关于inheritance - Xamarin.Forms:使用所有页面扩展 CommonToolbarPage 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49400795/

相关文章:

c# - 为什么这种继承不起作用?

.net - Protobuf-net : ProtoInclude and compatibility 中的继承

xamarin.forms - 如何在 ListView 中设置图像的大小?

c++ - 构造函数的巨型 switch 语句

C# 安卓 : Get Broadcast Receiver on a service?

c# - 如何从代码 Xamarin 将 TextView 添加到 StackView?

c# - Task<ObservableCollection<AppointmentItem>> 不包含 where 的定义

c# - 如何独立使用 Xamarin Profiler 来分析应用程序

facebook - 在服务器端Azure移动应用程序上提供 "login_hint"

entity-framework - 数据库的每个表中都有多个公共(public)字段 CreatedOn 和 CreatedBy。怎样才能不重复每张 table