隐藏特定ContentPage的StatusBar

时间:2016-04-06 11:14:14

标签: xamarin xamarin-forms

我正在创建一个应用,我想隐藏特定页面上的状态栏。在我的例子中,它是一个ContentPage。我找到了几个样本,其中info.plist用于隐藏它,但我只想要它用于特定页面,是否可能?使用NavigationPage.SetHasNavigationBar隐藏导航栏很容易,但状态栏似乎有点不同。

3 个答案:

答案 0 :(得分:18)

据我所知,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();
}

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

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

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

Info.plist

答案 1 :(得分:2)

您可以使用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))]

答案 2 :(得分:0)

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

    ...
}