如何在 Xamarin WPF 中使用 TabbedPage 更新 #1:我尝试构建 GTK 目标,它工作正常:

问题描述

我正在尝试使用 Xamarin Forms 重建我的旧 WinForms 应用程序以提高可移植性。

应用程序使用 TabControlthe docs 表示最接近的 Xamarin Forms 元素是 TabbedPage

代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:MyApp"
            x:Class="MyApp.MainPage">
    <TabbedPage.toolbaritems>
        <ToolbarItem Text="Settings"
                     Order="Primary"
                     Priority="0" />
        <ToolbarItem Text="Tools"
                     Order="Primary"
                     Priority="1" />
        <ToolbarItem Text="Help"
                     Order="Primary"
                     Priority="2" />
    </TabbedPage.toolbaritems>

    <TabbedPage.Children>
        <ContentPage Title="Main">
            <Label Text="Coming soon!"/>
        </ContentPage>

        <ContentPage Title="Today">
            <Label Text="Coming soon!"/>
        </ContentPage>

        <ContentPage Title="This week">
            <Label Text="Coming soon!"/>
        </ContentPage>

    </TabbedPage.Children>

</TabbedPage>

它在我的 UWP 版本上运行良好:

enter image description here

但是,在我的 WPF 版本中,它不显示工具栏,因此无法切换选项卡:

enter image description here

那么我在这里做错了什么?

谢谢。

更新 #1:我尝试构建 GTK 目标,它工作正常:

enter image description here

此屏幕截图是在 Ubuntu 上拍摄的。 Windows 上的 GTK# 工作方式相同,但控件的绘制有问题。

解决方法

弄乱工具栏中的元素选择器后,我终于找到了答案:

WPF 中有用于在选项卡之间切换的按钮。但是,它们只是不可见:enter image description here

BarTextColor 中设置 TabbedPage 属性可解决此问题: enter image description here

所以得到的代码是:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TimetableApp"
            x:Class="TimetableApp.MainPage" 
            BarTextColor="Black">
    <TabbedPage.ToolbarItems>
        <ToolbarItem Text="Settings"
                     Order="Primary"
                     Priority="0" />
        <ToolbarItem Text="Tools"
                     Order="Primary"
                     Priority="1" />
        <ToolbarItem Text="Help"
                     Order="Primary"
                     Priority="2" />
    </TabbedPage.ToolbarItems>

    <TabbedPage.Children>
        <ContentPage Title="Main">
            <Label Text="Coming soon!"/>
        </ContentPage>

        <ContentPage Title="Today">
            <Label Text="Coming soon!"/>
        </ContentPage>

        <ContentPage Title="This week">
            <Label Text="Coming soon!"/>
        </ContentPage>

    </TabbedPage.Children>

</TabbedPage>