如何在Xamarin表单中启用工具栏

3 c# xamarin.forms material-design

我有一个带有XAML的简单Xamarin项目,并且我想使用材质设计,默认情况下它似乎已启用,但工具栏未与MainPage一起显示。还是我需要在xaml上添加一个toobar?

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在关注教程。谢谢!

到目前为止,我刚刚得到这个... 在此处输入图片说明

小智 7

你应该换你ContentPageNavigationPage你的App.(xaml).cs

因此,当定义您的MainPage内部时,请App.(xaml).cs按照以下方式进行操作:

public App()
{
    InitializeComponent();
    MainPage = new NavigationPage(new MainPage());
}
Run Code Online (Sandbox Code Playgroud)

当你进一步导航,你就不必包裹ContentPageNavigationPage每一个时间,因为导航是相对的。