简体   繁体   中英

Initialize WP8 App

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.

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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM