首页 > 解决方案 > xamarin.forms 未显示 ToolbarItem

问题描述

我刚刚创建了空的 xamarin.forms 项目,其中没有任何内容。

我搜索了如何添加工具栏,在 Microsoft 文档中说将这些行添加到 xaml 文件。

    <ContentPage.ToolbarItems>
    <ToolbarItem Text="Example Item"
                 IconImageSource="example_icon.png"
                 Order="Primary"
                 Priority="0" />
    </ContentPage.ToolbarItems>

但是我的android设备上没有显示工具栏,这真的很伤心,因为这是我第一次使用xamarin,这只是一个空项目,但它不起作用。

这是我的主要 xaml 文件

 <?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="test3.MainPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="sssssssss Item"
                 Order="Primary"
                 Priority="0" />
    </ContentPage.ToolbarItems>
    <StackLayout>
        <!-- Place new controls here -->
        <Label Text="Welcome to Xamarin.Forms!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage>

标签: xamarin.forms

解决方案


您的 App.xaml.cs 应该与此类似:-

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

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

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }

推荐阅读