0

I want to initialize my app and start depending upon following conditions.

If(UserloggedIn)
    start from homepage.xaml
else
    login.xaml

I know there has to be a very simple and straight forward way for it as its a simple scenario. Please let me know the way.

LojiSmith
  • 282
  • 3
  • 20
  • Actually, that's not as simple as you think). First, I should ask: how do you check, if user is logged in. Send a web request to check credentials(that's the most likely solution, I can assume)? Second, the implementation of login page in windows-phone often has a issue with passing the Microsoft certification (will be problems with back button usage in app, I guarantee it) So you need to provide a bit more info about your implementation. – Olter Feb 20 '14 at 07:44
  • no, you are taking it much technical :p. i am saving userdetail in my isolatedstorage (just userid and name ;)) so no technical challenge. I just need to check user detail available in isolatedstorage then initialze from home home page otherwise show loginscreen of my app that communicates to my server and provides userid. – LojiSmith Feb 20 '14 at 07:46
  • I would suggest a UriMapper, this removes the need to clear backstacks, and do unneeded page navigation which can slow the app. There is an answer below which suggests this – Shawn Kendrot Feb 20 '14 at 15:30

3 Answers3

1

What you need to do is use a custom UriMapper, that will do the actual check if the user is logged in and tweak the navigation if needed.

A complete example is available on Shawn Kendrot his blog here: http://visuallylocated.com/2012/06/default.aspx

Depechie
  • 6,102
  • 24
  • 46
0

I think what you want is to edit the App.xaml.cs file. See here for more details.
You can also do some other useful things like checking if the app is a trial in App.xaml.cs.

Travis Liew
  • 787
  • 1
  • 11
  • 34
0

All right. I don't pretend my solution to be the simplest or the best. That's just the way, I've implemented it in my app.

First, you create a login page. That'll be the first page of your app. So, in WMAppManifest you set first page of app:

<Tasks>
  <DefaultTask Name="_default" NavigationPage="LoginPage.xaml" />
</Tasks>

Then, in login page constructor, you check, if user is logged in:

if (yourLoginCheck)
{
  NavigationService.Navigate(new Uri(MainPage, UriKind.RelativeOrAbsolute))
}

So if the user is logged in, he would be navigated to main page of app.

If the user isn't logged in, he should write his credentials at the login page UI. Then, if credentials check is ok, he'll also be navigated to login page.

That's the basic idea.


However, there are some issues, that should be solved. The primary one, is the fact, that user can use back button and return to login page, which is not cool.

So, I would recommend this solution: 1) You make login page controls invisible by default. 2) You override the OnNavigatedTo and OnBackKeyPress methods at login page:

  protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (e.NavigationMode == NavigationMode.Back)
            {
                App.Quit();
            }
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            App.Quit();
        }

3) If the user is logged in, he would be navigated to main page. If not, make credential controls at page visible.

In result, the user a) won't be able to see any credentials controls, if he's logged in. (the navigation will take only a few time, but he'll still be able to see login page, which'll be blank) b) Won't be able to return to login page by using back button. (instead the app will close)

btw, quit mehtod is:

 public static void Quit()
        {
            if (Environment.OSVersion.Version.Major < 8)//try to load XNA assemblies (only working on WP7)
            {
                System.Reflection.Assembly asmb = System.Reflection.Assembly.Load("Microsoft.Xna.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553");
                asmb = System.Reflection.Assembly.Load("Microsoft.Xna.Framework.Game, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553");
                Type type = asmb.GetType("Microsoft.Xna.Framework.Game");
                object obj = type.GetConstructor(new Type[] { }).Invoke(new object[] { });
                type.GetMethod("Exit").Invoke(obj, new object[] { });
            }
            else// => WP8
            {
                Type type = Application.Current.GetType();
                type.GetMethod("Terminate").Invoke(Application.Current, new object[] { });
            }
        }

that'll make app compatible with windows-phone-7.

The link, that may be helpful: About first page and quit issue

Community
  • 1
  • 1
Olter
  • 1,129
  • 1
  • 21
  • 40