Friday, 23 January 2015

Xamarin Forms Navigation–Dealing with Login

This is the third in a series of posts on Xamarin Forms – in this instalment I want to add login capability to my app, but this isn’t as simple as I thought it would be. I’m used to crafting interfaces with XAML in WPF and Silverlight, and have been doing so for several years now, but with Xamarin Forms things are a little different.

Here’s what I want to be able to do, courtesy of Visio…

image

When the app starts up I’ll check if the user has a valid token, and if so skip the login part and go straight to the main page.

If however the user hasn’t yet logged in I want to show a “carousel” where I can do some app advertising, then let them choose from one of the login providers I’ve integrated, and once logged in show the main form. The gnarly part here is the navigation support – once at the MainForm stage I don’t want to allow the user to “back out” into the login part, and so far on the intertubes I’ve not seen a clear way of doing this so looked into this myself.

I’m using Xamarin Forms 1.3 which has improved navigation support, so the first thing I tried was a main form that cleared out the navigation stack when it showed up. This worked to a fashion, in that the back-stack was cleared, but it left a “< Back” button on the navigation section when I arrived at the main form, and that was less than ideal.

After trying some other options (non of which panned out) I went back to first principles and looked at how the application startup sequence worked, and from that arrived at a simple solution – at the point where I need to display the main page, I simply call the following…

    var nav = new NavigationPage(new MainPage());
nav.BarBackgroundColor = Color.FromHex("#195174");
nav.BarTextColor = Color.White;

Application.Current.MainPage = nav;

And boom - the main page is shown and there's no previous pages that can be navigated to.

I’d originally created my main page and set that as the root, then coded the login forms as modal, and whilst that worked OK it had the undesired behaviour of showing the main form first (briefly), then navigating to the login page. That wasn’t particularly great as I want login to be the first thing seen when the application is run for the first time.

The downside of my approach here is that there’s no transition between the last login page and the main form, I haven’t found a way to do that at the moment.

I’m not sure if this is the best method either – however there doesn’t seem to be (or I cannot find) any alternatives that work quite how I want them to. I want to be able to use navigation during login, as I have a carousel page and then a provider selection page (and maybe more pages after that, once I’ve written them!), so using a Modal login page doesn’t suit my purposes.


Demo Code


If you would like to grab my example project please download it here. The structure is deliberately simple (I usually use Autofac and XAML in my projects, this has neither as there’s less to setup). When you run the app you’re on the carousel view which consists of three pages…


IMG_1304    IMG_1305    IMG_1306


I’m using a carousel here and a timer to move between the images, you can swipe too should you wish to. When you click on the “Providers” button a new page is shown, where you would ask the user to choose their login provider. I’ve cheated and just shown a login button…


IMG_1307


When the Login button is clicked I construct a new navigation page and the main page within it, and then set the application root page to this combination using the code shown above which gives the main page shown below.


IMG_1308




You can navigate elsewhere using the button…


IMG_1309  IMG_1310


And you can navigate directly back to the root page when you’re on the “At the end” page.


Conclusion


In this example I’ve attempted to show how you can “break out” of the navigation hierarchy and start a new navigation stack. There may be a better way to do this and if so please let me know!

27 comments:

Fredrik Haglund said...

Thanks for the research! I had exactly the same question on my plate and hopefully this approach will work for me too.

Morgan said...

Fredrik - you're welcome. Hopefully this will work well for you - and if there's a better way I'd love to hear about it!

asudbury said...

Really nice article and well thought out and explained - will certainly look to use in the future - thanks.

Erick Mendonça said...

Great! It helped me. :)

Dung Maurice said...

I think you could also use some of this tips on gps spy app and software

Robert F. Crocker said...


Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..

Magento 2 multiple filters

Ruth Sloan said...

Thanks for giving me the useful information. I think I need it! download blackmart market helper android free store apk

Michael Jones said...

Well done! It is one of the very informative blogs I have come across. You have a flair for writing and have fine knowledge of the topic as well. Students finding it tough to write their academic assignments could use our Assignment help and can get a first class coursework written from us.

Adam Thomas said...

This blog post is very different, I have searched this kind of post first time but even I fully enjoyed here when I was reading. AllAssignmenthelp Reviews

Stefan Carl said...

The higher education standard in Leeds is very high and attempting to make up for the lost expertise can get lower grades as a result.Homework help Leeds in students assignment help provide homework help to the student.

abcassignmenthelp said...

ABC Assignment Help is an incomparable online Accounting assignment help company delivering excellent academic assignments, essays, coursework and reports. Through a team of over 3000 subject experts we ensure individual attention to every student making the assignment help experience completely personalized in nature. With our round the clock services, you can be assured of high grades every time.

Unknown said...

If you are looking for reliable help with your CPM homework, get help at Myassignmenthelp. We have the perfect team of CPM homework helper at your service.

Kerrthika K said...

Nice post and more informative,thanks for sharing.
informatica mdm training
Informatica MDM Training in Chennai
Informatica Training Institute in Chennai
ionic course in chennai
IoT courses in Chennai
Data Analytics Courses in Chennai
Blockchain Training in Chennai

John32 said...

Having an assignment due tomorrow and understand you can’t do it yourself? But you’re afraid of delegating your paper to some scam services as well? Why not check edubirdie reviews on Scamfighter.net

james cook said...

Allassignmenthelp is a web portal where students get assignment expert help for all the subjects, with the help of our experts. You will get 100% plagiarism free assignment. Expert’s consultation is also available for students. If they have any query they can contact with our experts anytime.

MyAssignmentHelp said...


Thank you for sharing this informative post.MyEssayhelp is giving Essay help to students.we are already trusted by thousands of students who struggle to write their academic papers and also by those students who simply want .college essay writing service to save their time and make life easy.

Emma Brown said...

Your essay is too difficult and you need some help? Hiring somebody on the Internet doesn’t sound like a good idea? At EssayTopicsMasters we have found a solution to your dilemma. Check out https://essaytopicsmasters.com/reviews/homeworkmarket-com review and find the writing service you may rely on.

Phil Richards said...

If you search business resume template. Check useful article on resume101.org Where you will find out why a business development manager resume requires unique approach.

Puremelda said...

Our dissertation help services in USA and custom research paper writing in USA are meant to transform the academic profile of any student from a downward trend to an upward trend in their grades.

meldaresearch said...

Companies offering nursing paper writing service
should ensure that clients order their papers early so as to provide nursing writers time to write superior quality nursing term paper since we are the best in offering pre written essays for sale.

Roman said...

The nursing thesis or nursing dissertation is the most timeconsuming and voluminous among the research works of various nursing programs. Therefore, we offer you professional assistance with nursing research topics. Let us facilitate your life and save your time and good mood for you, for when you order your nursing research papers, you will receive the top quality work. A well-organized teamwork assures of the best result possible.

Puremelda said...

We deliver our nursing essay writing service ahead of deadlines and time frame you specify more often than not, having an insight on the significance of timely submission of the cheap term papers to institution departments.

meldaresearch said...

The good online research paper writer companies satisfy clients’ needs. When seeking for custom research paper writing service, learners should be very carefully go through the website’s content.If the content is carefully written, then learners should know that the company is legitimate.

nadiya said...

Nice Article you have posted here. Thank you for giving this innovative information and
please add more in future.
Xamarin Training in Chennai
Xamarin Training Institutes in Chennai
Best Spoken English Class in Chennai
TOEFL Coaching Centres in Chennai
french classes
pearson vue test center in chennai
Xamarin Training in OMR
Xamarin Training in Porur

Puremelda said...

Our hire Best Custom Writing writers understand the characteristics of proper writing needed in Custom Paper Writing Services.

meldaresearch said...

One of the things that you will realize is that our company offers custom Assignment Help Online that are similar to what you desire for you to improve your grade. Our writing urgency has specialized in offering Research Paper Writing Service services, and we have been in the industry for a decade.

Puremelda said...

It becomes necessary for students to identify companies that offer Cheap Term Paper Writers,to assist them in their assignments and Custom Dissertation Writing Help. The most convenient issue is that you can access the Custom Research Paper at any time and location as long as you have Internet connectivity.