【问题标题】:xamarin.forms not showing ToolbarItemxamarin.forms 未显示 ToolbarItem
【发布时间】:2020-03-15 20:06:02
【问题描述】:

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

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

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

但我的安卓设备上没有显示工具栏,这真的很伤心,因为这是我第一次使用 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>

【问题讨论】:

  • 您需要使用 NavigationPage 才能显示工具栏

标签: xamarin.forms


【解决方案1】:

您的 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
        }
    }

【讨论】:

  • 非常感谢,我认为微软应该在他们的文档中添加更多细节我怎么知道它是这样工作的,我曾经颤抖和离子,他们的文档很棒,但这个只是恼火我。
猜你喜欢
  • 2018-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-25
  • 2019-02-17
  • 1970-01-01
相关资源
最近更新 更多