为什么选项卡不在 Xamarin Forms 的底部?

Why the tabs are not in the bottom in Xamarin Forms?

各位开发者,

我正在使用 Xamarin.Forms 开发一个应用程序,我使用的是最新版本 3.1,据我所知它支持 Android 中的底部标签。这是我的代码:

XAML:

<?xml version="1.0" encoding="utf-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
    android:TabbedPage.ToolbarPlacement="Bottom"
    android:TabbedPage.BarItemColor="#666666"
    android:TabbedPage.BarSelectedItemColor="Black"
    BarBackgroundColor="#2196F3"
    BarTextColor="White"></TabbedPage>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace BottomTabsExample
{
    public partial class MainPage : TabbedPage
    {
        public MainPage()
        {
            var navigationPage = new NavigationPage(new History());
            navigationPage.Icon = "ic_history.png";
            navigationPage.Title = "History";

            var navigationPage2 = new NavigationPage(new History());
            navigationPage2.Icon = "ic_earth.png";
            navigationPage2.Title = "Earth";
            Children.Add(navigationPage);
            Children.Add(navigationPage2);
        }
    }
}

但是,它们总是显示在上部,如下图:

上图使用的是AndroidOreo 8.1(我也测试了7.1版本,结果一样)。我从这些博客中获得了部分示例:

https://montemagno.com/xamarin-forms-official-bottom-navigation-bottom-tabs-on-android/

https://blog.xamarin.com/xamarin-forms-3-1-improvments/

此外,.NET Standard 的版本和 Xamarin.Forms:

你们有没有经历过?有谁知道我做错了什么?感谢您的支持。

Why the tabs are not in the bottom in Xamarin Forms?

我发现您的 MainPage 中缺少 InitializeComponent(),这就是问题发生的原因。

InitializeComponent() 方法将加载 XAML 内容,当您删除此方法时,您的 XAML 内容将被忽略。

解决方案:

在后面的 C# 代码中设置底部标签:

using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;

On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);