I updated the samples and snippets to include some reader feedback:
Accounted for Master-Detail navigation in App.PromptToConfirmExit
Provided a toast + back button confirmation alternative sample
Updated sample: allow toggling between Master-Detail and Tabbed navigation; allow toggling between dialog and toast confirmation styles.
Overview
Ever hit the navigation bar back button when using an Android app and have it fully exit the app and drop you back on your home screen? I think all Android users have encountered this a few times. It is a frustrating and counter-productive experience. Let’s make sure your app has better UX by using a simple confirmation dialog!
Good news, this is easy to implement and should only take you a few minutes to code and test.
Step 1
Create a property to check if there are pages on your navigation stack. Here’s my implementation:
Step 2
Override the OnBackPressed method in your app’s MainActivity to intercept back navigation. Here’s a sample:
Inspired by Steven Thewissen’s excellent MSAL article, I thought I would share what I have learned about MSAL over the 3+ years I have worked with MSAL and Xamarin. If you’re just getting started, be sure to read Steven’s article, he does a great job covering how to use the tools. This article is a collection of my personal learnings and experience using MSAL in my apps.
Without further ado, here are 6 tips/tricks/facts about MSAL, AAD B2C and Xamarin:
TIP: Careful with package upgrades!
I cannot emphasize how important this is, check out the older packages in nuget: https://www.nuget.org/packages/Microsoft.Identity.Client/ the library was in preview for nearly 3 years and I went through the pain of dealing with api changes and behaviour changes. Read the change log carefully!
FACT: You cannot change the Android webview title from “Sign In” without a custom MSAL build.
This is roughly what I have used in my last few projects, not exact, I generally use dependency injection and a few other bits. For simplicity’s sake this is a self-contained sample implementation. I would advise using Lazy<T> for IPublicClientApplication at the very least in a real life application.
TLDR: The Experimental Mobile Blazor Bindings can result in much less XAML and C#, 64% less in my experiment, but the tech isn’t quite ready for prime time.
First off, Blazor is a new technology within ASP.NET Core which allows rich web applications to be written with C# instead of javascript (for the mostpart). My intent here isn’t to explain Blazor, if you wish to learn more, check out the resources listed at the end of this article.
Blazor uses the ASP.NET Razor template engine to deliver html content to browser clients. Html is markup, Xaml is markup…why not use the Razor engine to build Xamarin?
That’s the question Eilon Lipton answered with Mobile Blazor Bindings. In essence, this approach uses Razor syntax with Xamarin Forms Xaml to create mobile apps.
You may find yourself wondering why you should care…as I did when this was announced. I’m not a massive fan of Razor pages and lately I’m finding myself frustrated with the huge amount of Xaml code required to do some relatively simple things in Xamarin Forms. The Mobile Blazor Bindings approach doesn’t really solve the Razor pages issue, but it certainly helps simplify Xaml and at the same time, it also helps simplify your C# by using a binding which doesn’t rely on INotifyPropertyChanged.
I was not excited about this approach until I actually saw the results. There is huge upside and promise, I’m looking forward to seeing where this goes. No having to worry about INotifyPropertyChanged, no commanding, far more flexibility in Xaml with the tried and true Razor engine.
On the other side, I ran into 3 relatively major issues.
OnClick methods would only fire once
An IsVisible flag set by a variable wasn’t working at all
iOS wouldn’t work (I didn’t look very close at this).
Note: these issues are all fixed in the Preview 2 update!
There is also no hot reload support at this point, which you miss very fast after getting used to it.
Great job by Eilon Lipton and everyone else from the Xamarin and Blazor teams who brought this experiment to life!