共用方式為


StatusBarBehavior

StatusBarBehavior可讓您自定義裝置狀態列的色彩和樣式。

會在 StatusBarBehavior 更新屬性時套用色彩和樣式值。 這些值也會根據 ApplyOn 屬性套用,此屬性可讓您定義使用哪一個生命週期事件:

  • StatusBarApplyOn.OnBehaviorAttachedTo - 將行為附加至頁面時套用色彩和樣式。 這是預設值。
  • StatusBarApplyOn.OnPageNavigatedTo - 在瀏覽頁面時套用色彩和樣式。

注意

如果您的應用程式會根據每個頁面變更狀態列外觀,則您應該使用 StatusBarApplyOn.OnPageNavigatedTo 屬性的值 ApplyOn 。 否則,當流覽回系統時,會保留使用者從該頁面流覽而未流覽到的頁面的狀態列外觀。

重要

.NET MAUI 社群工具組行為不會設定 BindingContext 行為,因為行為可以透過樣式共用並套用至多個控件。 如需詳細資訊, 請參閱 .NET MAUI 行為

語法

XAML

包含 XAML 命名空間

若要在 XAML 中使用工具組,必須將下列專案 xmlns 新增至您的頁面或檢視:

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

因此,下列專案:

<ContentPage
    x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

</ContentPage>

將修改為包含 xmlns ,如下所示:

<ContentPage
    x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">

</ContentPage>

使用 StatusBarBehavior

StatusBarBehavior可以在 XAML 中使用,如下所示:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="MyLittleApp.MainPage">
    
    <ContentPage.Behaviors>
        <toolkit:StatusBarBehavior StatusBarColor="Fuchsia" StatusBarStyle="LightContent" />
    </ContentPage.Behaviors>

</ContentPage>

C#

StatusBarBehavior可以在 C# 中使用,如下所示:

class MyPage : ContentPage
{
    public MyPage()
    {
        this.Behaviors.Add(new StatusBarBehavior
        {
            StatusBarColor = Colors.Red,
            StatusBarStyle = StatusBarStyle.LightContent
        });
    }
}

有另一種方式可以存取 C# 上的 Statusbar API,您可以直接呼叫 方法,如下列代碼段所示:

class MyPage : ContentPage
{
    protected override void OnNavigatedTo(NavigatedToEventArgs args)
    {
        base.OnNavigatedTo(args);
        CommunityToolkit.Maui.Core.Platform.StatusBar.SetColor(statusBarColor);
        CommunityToolkit.Maui.Core.Platform.StatusBar.SetStyle(StatusBarStyle.LightContent);
    }
}

警告

如果您要新增此程式碼 MainPage的建構函式 OnAppearingOnNavigatedTo 方法,請改用 Behavior 。 直接在這些位置上使用 可能會當機您的應用程式,因為平臺特定元件可能無法初始化。

組態

不需要變更。

屬性

屬性 類型​ 描述
ApplyOn StatusBarBehavior 何時套用狀態列色彩和樣式。
StatusBarColor Color Color Microsoft.Maui.Graphics 命名空間的名稱。
StatusBarStyle StatusBarStyle 狀態欄所使用的樣式可以是 LightContent、DarkContent 或 Default。

範例

您可以在 .NET MAUI Community Toolkit 範例應用程式中找到此行為的範例。

API

您可以在 .NET MAUI Community Toolkit GitHub 存放庫找到 的StatusBarBehavior原始程式碼。