c# - 如何在 Xamarin Forms 中启用工具栏

标签 c# xamarin.forms material-design

我有一个带有 XAML 的简单 Xamarin 项目,我想使用 Material Design,它似乎默认启用,但工具栏没有与 MainPage 一起显示。还是我需要通过 xaml 添加工具栏?

主页.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>

样式.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>

工具栏.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" />

标签栏.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" />

主要事件.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());
    }
}

我正在关注 This教程。谢谢!

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

最佳答案

您应该将 ContentPage 包装在 App.(xaml).cs 中的 NavigationPage 中。

所以当在你的 App.(xaml).cs 中定义你的 MainPage 时,这样做:

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

当您进一步导航时,您不必每次都将 ContentPage 包装在 NavigationPage 中,因为导航是相对的。

关于c# - 如何在 Xamarin Forms 中启用工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39199170/

相关文章:

xamarin - 如何在Xamarin.Forms中创建多行TextBox

android - 5.1崩溃-TaskDescription的原色应该是不透明的

c# - 在 C# 中播放部分声音 (WMA) 文件

c# - 时间格式 C# - 防止月份被转换为母语

c# - SQL - 创建的数据库位置

c# - 为什么代码分析选项卡不显示在我的 Visual Studio 2010 Premium 项目属性中?

c# - 删除sqlite中的where in子句

xamarin - Android 以编程方式更改主题

primefaces - 将图标(例如 Material Design 或 Prime Icons)添加到 p :inputText

material-design - Flutter:如何让卡片可点击?