隐藏特定ContentPage的StatusBar [英] Hide StatusBar for specific ContentPage

查看:155
本文介绍了隐藏特定ContentPage的StatusBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,我想在该应用程序中隐藏特定页面上的状态栏.在我的示例中,这是一个ContentPage.我发现了几个示例,其中使用info.plist来隐藏它,但是我只希望将其用于特定页面,这可能吗?使用NavigationPage.SetHasNavigationBar隐藏导航栏很容易,但是状态栏似乎有些不同.

I'm creating an app where I want to hide the statusbar on a specific page. In my example it's a ContentPage. I found several samples where the info.plist is used to hide it, but I only want it for a specific page, is it possible? It's easy to hide the navigationbar with NavigationPage.SetHasNavigationBar, but statusbar seems a bit different.

推荐答案

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

As far as I know, Xamarin doesn't provide this functionality through Xamarin.Forms classes. You will need to implement it in each platform specific project.

但是,这应该相当容易,因为您可以使用 DependencyService 处理此问题.

However, this should be fairly easy as you can use a DependencyService to handle this.

这是一个快速的实现方法...

Here is a quick implementation...

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();
}

AndroidImplementation

[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
    }
}

iOSImplementation

[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实现来隐藏状态栏.隐藏之后(可能不确定),您可能还需要再次显示它.

The idea being that you call the DependencyService implementation to hide the status bar when you need it hidden on a specific ContentPage. You also may need to show it again after hiding(not really sure).

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

NOTE: For iOS, you need to update the Info.plist file to allow the application to change the status bar visibility.

这篇关于隐藏特定ContentPage的StatusBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆