问题描述
我正在尝试使用 Xamarin Forms 重建我的旧 WinForms 应用程序以提高可移植性。
应用程序使用 TabControl
,the 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 版本上运行良好:
但是,在我的 WPF 版本中,它不显示工具栏,因此无法切换选项卡:
那么我在这里做错了什么?
谢谢。
更新 #1:我尝试构建 GTK 目标,它工作正常:
此屏幕截图是在 Ubuntu 上拍摄的。 Windows 上的 GTK# 工作方式相同,但控件的绘制有问题。
解决方法
弄乱工具栏中的元素选择器后,我终于找到了答案:
WPF 中有用于在选项卡之间切换的按钮。但是,它们只是不可见:
在 BarTextColor
中设置 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: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>