隐藏特定 ContentPage 的 StatusBar

Hide StatusBar for specific ContentPage

我正在创建一个应用程序,我想在其中隐藏特定页面上的状态栏。在我的示例中,它是一个 ContentPage。我发现了几个示例,其中 info.plist 用于隐藏它,但我只希望它用于特定页面,这可能吗?用NavigationPage.SetHasNavigationBar隐藏导航栏很简单,但是状态栏好像有点不一样。

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);
    this.Window.ClearFlags(WindowManagerFlags.Fullscreen);
    SetContentView (Resource.Layout.Main);

    ...
}

据我所知,Xamarin 不通过 Xamarin.Forms 类 提供此功能。您将需要在每个特定于平台的项目中实施它。

但是,这应该相当容易,因为您可以使用 DependencyService 来处理它。

这是一个快速实现...

App.cs

public App()
{
    var layout = new StackLayout
    {
        VerticalOptions = LayoutOptions.Center,
        Children =
        {
            new Label
            {
                XAlign = TextAlignment.Center,
                Text = "Welcome to Xamarin Forms!"
            }
        }
    };

    var button = new Button
    {
        Text = "Click Me"
    };
    button.Clicked += (object sender, EventArgs e) => 
        {
            if (_isHidden)
            {
                // show
                DependencyService.Get<IStatusBar>().ShowStatusBar();
            }
            else
            {
                // hide
                DependencyService.Get<IStatusBar>().HideStatusBar();
            }

            _isHidden = !_isHidden;
        };

    layout.Children.Add(button);

    // The root page of your application
    MainPage = new ContentPage
    {
        Content = layout
    };
}

IStatusBar.cs

public interface IStatusBar
{
    void HideStatusBar();
    void ShowStatusBar();
}

Android实现

[assembly: Xamarin.Forms.Dependency(typeof(StatusBarImplementation))]
namespace MyXamarinApp.Droid
{
    public class StatusBarImplementation : IStatusBar
    {
        public StatusBarImplementation()
        {
        }

        WindowManagerFlags _originalFlags;

        #region IStatusBar implementation

        public void HideStatusBar()
        {
            var activity = (Activity)Forms.Context;
            var attrs = activity.Window.Attributes;
            _originalFlags = attrs.Flags;
            attrs.Flags |= Android.Views.WindowManagerFlags.Fullscreen;
            activity.Window.Attributes = attrs;
        }

        public void ShowStatusBar()
        {
            var activity = (Activity)Forms.Context;
            var attrs = activity.Window.Attributes;
            attrs.Flags = _originalFlags;
            activity.Window.Attributes = attrs;
        }

        #endregion
    }
}

iOS实施

[assembly: Xamarin.Forms.Dependency(typeof(StatusBarImplementation))]
namespace MyXamarinApp.iOS
{
    public class StatusBarImplementation : IStatusBar
    {
        public StatusBarImplementation()
        {
        }

        #region IStatusBar implementation

        public void HideStatusBar()
        {
            UIApplication.SharedApplication.StatusBarHidden = true;
        }

        public void ShowStatusBar()
        {
            UIApplication.SharedApplication.StatusBarHidden = false;
        }

        #endregion
    }
}

当您需要在特定 ContentPage 上隐藏状态栏时,您可以调用 DependencyService 实现来隐藏状态栏。您也可能需要在隐藏后再次显示它(不太确定)。

注意:对于 iOS,您需要更新 Info.plist 文件以允许应用程序更改状态栏可见性。

您可以为此使用 PageRenderer。这是一个例子:

public class NoStatusBarPageRenderer : PageRenderer
{
    public NoStatusBarPageRenderer()
    {
    }

    public override void ViewWillAppear(bool animated)
    {
        UIApplication.SharedApplication.SetStatusBarHidden(true, UIStatusBarAnimation.Fade);

        base.ViewWillAppear(animated);
    }

    public override void ViewDidDisappear(bool animated)
    {
        UIApplication.SharedApplication.SetStatusBarHidden(false, UIStatusBarAnimation.Fade);

        base.ViewDidDisappear(animated);
    }
}

然后,对于您希望隐藏状态栏的每个页面,添加一个属性以在该页面上使用此渲染器。

[assembly: ExportRenderer(typeof(MyContentPage), typeof(NoStatusBarPageRenderer))]