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