question

GordonS-9701 avatar image
0 Votes"
GordonS-9701 asked GordonS-9701 commented

Xamarin Forms show lock page in sleep mode

I have a banking app on my phone that switches to a plain (just some text) page when in "overview" mode - i.e. when you are scrolling through all open apps. I want to add the same to my app. So, when in sleep mode (and being viewed in overview mode) show a lock / text page. When the app resumes, go back to the page they were on.

I thought that would be simple, but then I am new to Xamarin! I tried:

         Shell.Current.GoToAsync($"//{nameof(LockPage)}");

in OnSleep and

         Shell.Current.GoToAsync("..");

in OnResume


But that seemed to have no effect. I've searched Google for ages and not found any examples.

Is this the correct approach, or am I in the wrong place completely?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered GordonS-9701 commented

Hello,​

Welcome to our Microsoft Q&A platform!

To achieve Secure Background Screen in Xamarin, you do not need to use sleep method.

For android. Open your MainActivity.cs add Window.SetFlags(WindowManagerFlags.Secure, WindowManagerFlags.Secure); in your OnCreate() method.

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
            Window.SetFlags(WindowManagerFlags.Secure, WindowManagerFlags.Secure);
        }


For iOS. The easiest is to set a blur when the AppDelegate calls OnResignActivation. Place the following in your AppDelegate of your Xamarin.iOS app. This creates a blur effect on the main window, when OnResignActivation is called, then removes it, when OnActivated is called.

UIVisualEffectView _blurWindow = null;

public override void OnActivated(UIApplication application)
{
    base.OnActivated(application);

    _blurWindow?.RemoveFromSuperview();
    _blurWindow?.Dispose();
    _blurWindow = null;
}

public override void OnResignActivation(UIApplication application)
{
    base.OnResignActivation(application);

    using (var blurEffect = UIBlurEffect.FromStyle(UIBlurEffectStyle.Dark))
    {
        _blurWindow = new UIVisualEffectView(blurEffect)
        {
            Frame = UIApplication.SharedApplication.KeyWindow.RootViewController.View.Bounds
        };
        UIApplication.SharedApplication.KeyWindow.RootViewController.View.AddSubview(_blurWindow);
     }
}


Here is a blog about it.

======================Update==============================


For IOS, I found we can custom this view, For example. we can add an icon to the Secure Background Screen. I Add a image to the UIVisualEffectView

public override void OnActivated(UIApplication application)
        {
            base.OnActivated(application);

            _blurWindow?.RemoveFromSuperview();
            _blurWindow?.Dispose();
            _blurWindow = null;
        }

        public override void OnResignActivation(UIApplication application)
        {
            base.OnResignActivation(application);

            using (var blurEffect = UIBlurEffect.FromStyle(UIBlurEffectStyle.Dark))
            {
                _blurWindow = new UIVisualEffectView(blurEffect)
                {
                    Frame = UIApplication.SharedApplication.KeyWindow.RootViewController.View.Bounds
                };

                UIImageView image = new UIImageView();
                image.Frame = new CoreGraphics.CGRect(100, 100, 40, 40);
                image.Image = UIImage.FromFile("icon.png");
                _blurWindow.ContentView.Add(image);
                UIApplication.SharedApplication.KeyWindow.RootViewController.View.AddSubview(_blurWindow);
            }
        }


Here is running screenshot.

79549-image.png

Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (138.4 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks, that is very useful information.

However, I have an iPhone and when my Barclays Bank app is in the background, it shows a plain white page with their logo and the word Barclays. The PayPal app does the same thing. That is what I'd like to do with my app.

Are they doing something super clever, or something that isn't possible with Xamarin?

0 Votes 0 ·

Because they are using native developed way, we used Shell in Xamarin.Forms, I make a test with OnSleep method and run in the UI thread, but it executes the navigate operation when back to the applications, the view do not update when in sleep mode. So If you use Shell, the above answer's way is easier.

0 Votes 0 ·

OK, so is it not possible to achieve the "native" solution by coding in each platform project (iOS and Android)?

Of course, your solution will suffice as a minimum, but I just know my boss is going to ask for a nice screen to be displayed!! :-)

0 Votes 0 ·
Show more comments