Windows 上的 TabbedPage 圖示

Download Sample 下載範例

此 通用 Windows 平台 平臺特定可讓頁面圖示顯示在工具列上TabbedPage,並提供選擇性地指定圖示大小的能力。 在 XAML 中,它會藉由將附加屬性設定 TabbedPage.HeaderIconsEnabledtrue來取用,並選擇性地將 TabbedPage.HeaderIconsSize 附加屬性設定為 Size 值:

<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 icons enabled platform-specific