首页 > 解决方案 > Adding ToolbarItems to MainPage.xaml.cs in a Xamarin.Forms Project

问题描述

As described in the title I am looking to add a ToolbarItem to my MainPage.xaml.cs in a Xamarin.Forms Project.

This is because I want an Actionbar on my Android App.

This to my knowledge is available on a NavigationPage.

I have not been able to set my Mainpage as a NavigationPage despite many attempts, it appears it will not let me set it as anything other than a ContentPage.

So far the best information I can find on this subject is here:

xamarin.forms not showing ToolbarItem

This however does not answer my question as it is relevant to App.xaml.cs not MainPage.xaml.cs

And my project is a multi-platform Xamarin.Forms for building apps apps for IOS and Android with Xamarin and Xamarin.Forms project.

Currently however I'm just focusing on Android in the shared C# project (MainPage.xaml.cs).

Thanks in Advance.

标签: c#androidxamlxamarinxamarin.forms

解决方案


ToolbarItems 需要一个导航页面来显示它们。因此,修改由导航页面包裹的 MainPage:

应用程序.xaml.cs:

  public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        MainPage = new NavigationPage(new MainPage());
    }

截屏: 在此处输入图像描述


推荐阅读