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