使用 NavigationService 时出现 NullreferenceException [英] NullreferenceException when using NavigationService

查看:25
本文介绍了使用 NavigationService 时出现 NullreferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 WP7 应用程序遇到问题:

I'm encountering a problem with my WP7 app:

仅当应用程序无法检测到任何存储的用户名或密码值时,我才尝试制作登录页面提示;但是,当我尝试导航到我的登录页面时,我一直遇到 NullReferenceException.

I'm trying to make a login page prompt only when the app can't detect any stored username or password values; however, I keep running into a NullReferenceException when I try to navigate to my login page.

我的代码看起来像这样,它在构造函数中:

My code looks like this, and it's in the contructor:

if (!checkLogin())
    {
        NavigationService.Navigate(new Uri("/LoginPage.xaml", UriKind.Relative));
    }

并且 checkLogin 只是一个返回 true 或 false 的函数,具体取决于隔离存储设置是否正确设置.

And checkLogin is just a function that returns either true or false depending or not the isolated storage settings are set correctly.

有人有什么建议吗?

谢谢.

推荐答案

这是我认为您想要做的 (来自 Peter Torr 的博客).

Here is what i think you want to do (From Peter Torr's blog).

如果您需要更多说明,这里有一段代码来说明这一点.假设有 2 个页面 A.XAML 和 B.xaml,您想根据检查存储在孤立存储中的一些登录凭据来检测是否加载 A.xaml 或 B.xaml,

If you need more clarification here is a piece of code to illustrate that. Assume there are 2 pages A.XAML and B.xaml and you would want to detect in whether to load A.xaml or B.xaml based on checking some login credentials which is stored in the IsolatedStorage,

在您项目的 App.xaml.cs 中覆盖 public App():

    public App()
    {
        // Global handler for uncaught exceptions. 
        UnhandledException += Application_UnhandledException;

        // Standard Silverlight initialization
        InitializeComponent();

        // Phone-specific initialization
        InitializePhoneApplication();

        // Show graphics profiling information while debugging.
        if (System.Diagnostics.Debugger.IsAttached)
        {
            // Display the current frame rate counters.
            Application.Current.Host.Settings.EnableFrameRateCounter = true;

            // Show the areas of the app that are being redrawn in each frame.
            //Application.Current.Host.Settings.EnableRedrawRegions = true;

            // Enable non-production analysis visualization mode, 
            // which shows areas of a page that are handed off to GPU with a colored overlay.
            //Application.Current.Host.Settings.EnableCacheVisualization = true;

            // Disable the application idle detection by setting the UserIdleDetectionMode property of the
            // application's PhoneApplicationService object to Disabled.
            // Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
            // and consume battery power when the user is not using the phone.
            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
        }

        RootFrame.Navigating += new NavigatingCancelEventHandler(RootFrame_Navigating);
    }

    void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
    {
        if (e.Uri.ToString().Contains("/MainPage.xaml") != true)
        {
            return;
        }
        e.Cancel = true;
        RootFrame.Dispatcher.BeginInvoke(delegate
        {
            if (System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Contains("Login_Credentials"))
            {
                RootFrame.Navigate(new Uri("/B.xaml", UriKind.Relative));
            }
            else
            {
                RootFrame.Navigate(new Uri("/A.xaml", UriKind.Relative));
            }
        });
    }

然后创建 2 个虚拟页面 A.xamlB.xaml 以便对于 A.xaml 你有一些逻辑来保存登录凭据(在这种情况下只是一个布尔标志):

Then create the 2 dummy pages A.xaml and B.xaml so that for A.xaml you have some logic for saving the login credentials (in this case just a boolean flag):

A.XAML:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock x:Name="PageTitle" Text="A Page" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Button Content="Save Login Creds" Click="SaveLoginCreds"/>
    </StackPanel>
</Grid>

A.XAML.cs:

private void SaveLoginCreds(object sender, RoutedEventArgs e)
{
    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Add("Login_Credentials", true);
}

现在,当您第一次运行应用程序时,它会加载 A.xaml,因为它找不到任何登录凭据.然后,如果您单击该按钮,它会将登录凭据数据保存在 IndependentStorage 中.下次启动应用时,它会加载 B.xaml,因为它检测到登录凭据.

Now, when you run the application for the first time, it will load A.xaml because it could not find any login credentials. Then if you click on the button, it will save the login credentials data in IsolatedStorage. Next time, you start the app it will load B.xaml because it detected the login credentials.

我希望这会有所帮助.

这篇关于使用 NavigationService 时出现 NullreferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆