如何在Xamarin Forms中启用工具栏

3

我有一个使用XAML创建的简单的Xamarin项目,想要使用Material Design,但似乎默认情况下并没有显示工具栏(toolbar)。我需要在XAML中添加一个工具栏吗?

MainPage.xaml:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Material"
             x:Class="Material.MainPage"> 
  <Label Text="Welcome to Xamarin Forms!"
           VerticalOptions="Center"
           HorizontalOptions="Center" />
  <ToolbarItem Name="Test" /> 
</ContentPage>

style.xml :

 <style name="MainTheme" parent="MainTheme.Base">
  </style>
  <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">#2196F3</item>
    <item name="colorPrimaryDark">#1976D2</item>
    <item name="colorAccent">#FF4081</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
  </style>
  <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#FF4081</item>
  </style>

Toobar.axml :

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

Tabbar.axml

<android.support.design.widget.TabLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="@android:color/white"
app:tabGravity="fill"
app:tabMode="fixed" />

MainActivity.cs :

[Activity(Label = "Material", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);
        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }
}

我正在跟随这个教程。谢谢!

到目前为止,我只得到了这个... enter image description here


正如我在这里提到的教程,ActionBar已经消失了,现在是AppCompat v7,他在教程中创建了工具栏。 - user3796130
1个回答

10

在你的App.(xaml).cs中,应该在ContentPage外部包裹一个NavigationPage

因此,在App.(xaml).cs中定义你的MainPage时,请按照以下方式进行:

public App()
{
    InitializeComponent();
    MainPage = new NavigationPage(new MainPage());
}

当您导航更深入时,您无需每次将ContentPage包装在NavigationPage中,因为导航是相对的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接