Windows Phone 7·动态改变Windows Phone 7的初始页面

Windows Phone 7的默认页面是在名叫WMAppManifest.xml文件里配置的
<Tasks>

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

</Tasks>

假如在启动程序的时候根据状态改变初始页面,比如程序在启动的时候判断用户是否登录,如果未登录则跳转到LoginPage.xaml否则跳转到其他界面。

代码如下:
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
{
return;
}
// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
RootFrame = new PhoneApplicationFrame();
RootVisual = RootFrame;
RootFrame.Navigated += CompleteInitializePhoneApplication;
// Handle navigation failures
RootFrame.NavigationFailed += RootFrame_NavigationFailed;
RootFrame.Navigating += new NavigatingCancelEventHandler(RootFrame_Navigating);//加上Navigating的事件
// Ensure we don't initialize again
phoneApplicationInitialized = true;
}
void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (e.Uri.ToString().Contains("LoginPage.xaml") && !Api.IsVerify)//这里一定要有判断条件,因为RootFrame在任何页面进行Navigate的时候都会掉用Navigating,或者采用跟RootFrame.Navigated 方式一样的递归方法
{
e.Cancel = true;//取消事件
string uriString = "/LoginPage.xaml";
Uri ur = new Uri(uriString, UriKind.Relative);
this.RootFrame.Dispatcher.BeginInvoke(delegate
{
this.RootFrame.Navigate(ur);
});
}
}
posted on 2011-12-19 08:59  Kingly  阅读(568)  评论(1编辑  收藏  举报