在没有父页面的情况下从一个页面重定向到另一个页面

时间:2014-03-11 09:38:47

标签: c# xaml windows-phone-8 parse-platform

我在从不同的页面而不是MainPage.xaml启动Windows phone应用程序时遇到了麻烦,但是动态地。这是代码:

public MainPage()
{
    InitializeComponent();

    if (appSettings["Parse.CurrentUser"] != null)
    {
        MessageBoxResult result = MessageBox.Show("Welcome Back","Welcome",MessageBoxButton.OK);
        this.NavigationService.Navigate(new Uri("/email.xaml", UriKind.RelativeOrAbsolute));
        //Above step causes error as a Null Exception. :(
    }
    else
    {
        // show the signup or login screen
    }
}

因此,如果用户已经被放入我不想显示MainPage而是我想要显示email.xaml页面。请帮助NullException

3 个答案:

答案 0 :(得分:1)

将此功能添加到app.xaml.cs.并在App构造函数

中调用它
    void SetupUriMapper()
    {
        // Get the UriMapper from the app.xaml resources, and assign it to the root frame
        UriMapper mapper = Resources["mapper"] as UriMapper;
        RootFrame.UriMapper = mapper;

        // Our dummy check -- does the current time have an odd or even number of seconds?


        // Update the mapper as appropriate
        if (!IsolatedStorageSettings.ApplicationSettings.Contains("id"))
            mapper.UriMappings[0].MappedUri = new Uri("/LoginPage.xaml?method=UriMapper", UriKind.Relative);
        else
            mapper.UriMappings[0].MappedUri = new Uri("/MainPage1.xaml?method=UriMapper", UriKind.Relative);
    }

像这样更新您的App.xaml

    <Application 
x:Class="YourApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"       
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:UriMapper="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone">
<!--xmlns:unsupported="clr-namespace:Microsoft.Phone.Controls.Unsupported">-->
<!--Application Resources-->
<Application.Resources>

    <UriMapper:UriMapper x:Name="mapper">
        <UriMapper:UriMapping Uri="/MainPage.xaml" />
    </UriMapper:UriMapper>

</Application.Resources>

<Application.ApplicationLifetimeObjects>
    <!--Required object that handles lifetime events for the application-->
    <shell:PhoneApplicationService 
        Launching="Application_Launching" Closing="Application_Closing" 
        Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>

答案 1 :(得分:1)

页面构造函数中尚未提供

NavigationService。尝试将代码从页面的构造函数移动到页面的OnNavigatedTo()事件:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (appSettings["Parse.CurrentUser"] != null)
    {
        MessageBoxResult result = MessageBox.Show("Welcome Back","Welcome",MessageBoxButton.OK);
        this.NavigationService.Navigate(new Uri("/email.xaml", UriKind.RelativeOrAbsolute));
    }
    else
    {
        // show the signup or login screen
    }
}

答案 2 :(得分:0)

在App.xaml.cs页面中,查看以下内容,

private void Application_Launching(object sender, LaunchingEventArgs e)
        {

            if (IsolatedStorageSettings.ApplicationSettings.Contains("Parse.CurrentUser"))
            {
                NavigationService.Navigate(new Uri("/email.xaml", UriKind.Relative));
            }
        }