Windows 上的 TabbedPage 图标

下载示例 下载示例

此特定于平台的通用 Windows 平台使页面图标显示在工具栏上TabbedPage,并提供选择性地指定图标大小的功能。 通过将附加属性设置为 TabbedPage.HeaderIconsEnabled ,并选择性地将附加属性true设置为Size值,即可在 XAML 中使用TabbedPage.HeaderIconsSize它:

<TabbedPage ...
            xmlns:windows="clr-namespace:Xamarin.Forms.PlatformConfiguration.WindowsSpecific;assembly=Xamarin.Forms.Core"
            windows:TabbedPage.HeaderIconsEnabled="true">
    <windows:TabbedPage.HeaderIconsSize>
        <Size>
            <x:Arguments>
                <x:Double>24</x:Double>
                <x:Double>24</x:Double>
            </x:Arguments>
        </Size>
    </windows:TabbedPage.HeaderIconsSize>
    <ContentPage Title="Todo" IconImageSource="todo.png">
        ...
    </ContentPage>
    <ContentPage Title="Reminders" IconImageSource="reminders.png">
        ...
    </ContentPage>
    <ContentPage Title="Contacts" IconImageSource="contacts.png">
        ...
    </ContentPage>
</TabbedPage>

或者,可以使用 Fluent API 从 C# 使用它:

using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;
...

public class WindowsTabbedPageIconsCS : Xamarin.Forms.TabbedPage
{
  public WindowsTabbedPageIconsCS()
  {
    On<Windows>().SetHeaderIconsEnabled(true);
    On<Windows>().SetHeaderIconsSize(new Size(24, 24));

    Children.Add(new ContentPage { Title = "Todo", IconImageSource = "todo.png" });
    Children.Add(new ContentPage { Title = "Reminders", IconImageSource = "reminders.png" });
    Children.Add(new ContentPage { Title = "Contacts", IconImageSource = "contacts.png" });
  }
}

TabbedPage.On<Windows>方法指定此特定于平台的仅在通用 Windows 平台上运行。 TabbedPage.SetHeaderIconsEnabled命名空间中的 Xamarin.Forms.PlatformConfiguration.WindowsSpecific 方法用于打开或关闭标题图标。 方法 TabbedPage.SetHeaderIconsSize (可选)使用 值指定标头图标大小 Size

此外, TabbedPage 命名空间中的 Xamarin.Forms.PlatformConfiguration.WindowsSpecific 类还具有 EnableHeaderIcons 启用标头图标的方法、 DisableHeaderIcons 禁用标头图标的方法和 IsHeaderIconsEnabled 返回 boolean 指示是否启用标头图标的值的方法。

结果是页面图标可以显示在工具栏上 TabbedPage ,图标大小可以选择设置为所需的大小:

TabbedPage 图标已启用特定于平台的