Full immersion with dependency service

JonathanZunigaJonathanZuniga USMember ✭✭
edited May 2015 in Xamarin.Forms

Hello, is it possible to implement this code with a dependency service?

public override void OnWindowFocusChanged (bool hasFocus)
{
        base.OnWindowFocusChanged (hasFocus);

        if (hasFocus && (int)Build.VERSION.SdkInt >= 19) {
            Window.DecorView.SystemUiVisibility =
                (StatusBarVisibility)(SystemUiFlags.LayoutStable
                    | SystemUiFlags.HideNavigation
                    | SystemUiFlags.LayoutFullscreen
                    | SystemUiFlags.Fullscreen
                    | SystemUiFlags.ImmersiveSticky);
        }
}

How? Any thoughts?

Best Answer

Answers

  • JonathanZunigaJonathanZuniga USMember ✭✭
    edited May 2015

    I have this code:

    IFullImmersion.cs

    using System;
    
    namespace FullscreenModeTest
    {
        public interface IFullImmersion
        {
            void Fullscreen (bool hasFocus);
        }
    }
    

    FullImmersion.cs

    using System;
    using Xamarin.Forms;
    using FullscreenModeTest.Droid;
    using Android.OS;
    using Android.Views;
    
    [assembly: Dependency (typeof (FullImmersion))]
    
    namespace FullscreenModeTest.Droid
    {
        public class FullImmersion : IFullImmersion
        {   
            public FullImmersion ()
            {
            }
    
            public void Fullscreen (bool hasFocus)
            {
                if (hasFocus && (int)Build.VERSION.SdkInt >= 19) {
                    Android.Views.Window.DecorView.SystemUiVisibility = 
                        (StatusBarVisibility)(SystemUiFlags.LayoutStable
                        | SystemUiFlags.HideNavigation
                        | SystemUiFlags.LayoutFullscreen
                        | SystemUiFlags.Fullscreen
                        | SystemUiFlags.ImmersiveSticky);
                }
            }
        }
    }
    

    ConfirmationPage.cs

    using System;
    using Xamarin.Forms;
    
    namespace FullscreenModeTest
    {
        public class ConfirmationPage : ContentPage
        {
            public ConfirmationPage ()
            {
                DependencyService.Get<IFullImmersion>().Fullscreen(true);
    
                Content = new StackLayout { 
                    BackgroundColor = Color.FromHex("#00c853"),
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    VerticalOptions = LayoutOptions.FillAndExpand,
                    Children = {
                        new Image {
                            HorizontalOptions = LayoutOptions.CenterAndExpand,
                            Source = "ic_done_white_24dp.png",
                            VerticalOptions = LayoutOptions.CenterAndExpand
                        }
                    }
                };
            }
        }
    }
    

    But when I try to compile it, it throws me this error: Error CS0120: An object reference is required to access non-static member `Android.Views.Window.DecorView' (CS0120) (FullscreenModeTest.Droid).

  • AndrewMobileAndrewMobile USMember ✭✭✭✭

    per the error, you need an instance to set the DecorView member. DecorView is a non-static property of the Window object.
    so you need something like this:

    activity.Window.DecorView;

    where activity is an Activity instance

  • JonathanZunigaJonathanZuniga USMember ✭✭

    @AndreiNitescu Thanks for your response, I get an exception when i try to open ConfirmationPage: java.lang.reflect.InvocationTargetException.

    i changed the code like below.

    using System;
    using Xamarin.Forms;
    using FullscreenModeTest.Droid;
    using Android.OS;
    using Android.Views;
    using Android.App;
    
    [assembly: Dependency (typeof (FullImmersion))]
    
    namespace FullscreenModeTest.Droid
    {
        public class FullImmersion : IFullImmersion
        {   
            public FullImmersion ()
            {
            }
    
            public void Fullscreen (bool hasFocus)
            {
                Activity activity = new Activity();
    
                if (hasFocus && (int)Build.VERSION.SdkInt >= 19) {
                    activity.Window.DecorView.SystemUiVisibility = 
                        (StatusBarVisibility)(SystemUiFlags.LayoutStable
                        | SystemUiFlags.HideNavigation
                        | SystemUiFlags.LayoutFullscreen
                        | SystemUiFlags.Fullscreen
                        | SystemUiFlags.ImmersiveSticky);
                }
            }
        }
    }
    
  • JonathanZunigaJonathanZuniga USMember ✭✭

    @AndreiNitescu Yes it works! :) Thank you very much.

Sign In or Register to comment.