首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NavigationService时的NullreferenceException

使用NavigationService时的NullreferenceException
EN

Stack Overflow用户
提问于 2012-09-14 23:29:00
回答 1查看 1.8K关注 0票数 0

我的WP7应用遇到了一个问题:

我尝试仅在应用程序检测不到任何存储的用户名或密码值时才显示登录页面提示;但是,当我尝试导航到登录页面时,我总是遇到NullReferenceException。

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

代码语言:javascript
复制
if (!checkLogin())
    {
        NavigationService.Navigate(new Uri("/LoginPage.xaml", UriKind.Relative));
    }

checkLogin只是一个返回true或false的函数,这取决于独立存储设置是否设置正确。

有人有什么建议吗?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-15 00:10:56

这是我认为你想要做的(From Peter Torr's blog)。

如果您需要更多说明,这里有一段代码来说明这一点。假设有2个页面A.XAML和B.xaml,并且您想要基于检查存储在IsolatedStorage中的一些登录凭证来检测是加载A.xaml还是B.xaml,

在项目的App.xaml.cs中,使用以下命令覆盖public App()

代码语言:javascript
复制
    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));
            }
        });
    }

然后创建两个虚拟页面A.xamlB.xaml,这样对于A.xaml,您就有了一些用于保存登录凭证的逻辑(在本例中只有一个布尔标志):

A.XAML:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
private void SaveLoginCreds(object sender, RoutedEventArgs e)
{
    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Add("Login_Credentials", true);
}

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

我希望这能帮到你。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12427601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档