简体   繁体   English

Template10 NavigationService为空

[英]Template10 NavigationService is null

This is my App.xaml.cs 这是我的App.xaml.cs

[Bindable]
sealed partial class App : Template10.Common.BootStrapper
{
    public App()
    {
        InitializeComponent();
        SplashFactory = (e) => new Views.Splash(e);

        var _settings = SettingsService.Instance;
        RequestedTheme = _settings.AppTheme;
        CacheMaxDuration = _settings.CacheMaxDuration;
        ShowShellBackButton = _settings.UseShellBackButton;
    }

    public override async Task OnInitializeAsync(IActivatedEventArgs args)
    {
        if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar"))
        {
            var statusBar = StatusBar.GetForCurrentView();
            await statusBar.HideAsync();
        }

        await Task.CompletedTask;
    }

    public override async Task OnStartAsync(StartKind startKind, IActivatedEventArgs args)
    {
        NavigationService.Navigate(typeof(Views.MainPage)); // Exception here
        await Task.CompletedTask;
    }
}

Every time i launch the app i get this exception: 每次启动该应用程序时,都会出现以下异常:

System.NullReferenceException verificata HResult=-2147467261 Message=Object reference not set to an instance of an object. System.NullReferenceException verificata HResult = -2147467261消息=对象引用未设置为对象的实例。

NavigationService is always null. NavigationService始终为null。

How can I solve this? 我该如何解决?

I have had similar issue, but inside of ViewModel not in App.xaml.cs. 我遇到过类似的问题,但是在ViewModel内部不在App.xaml.cs中。

So NavigationService was null when I defined and initialized my ViewModel as property in code behind of view. 因此,当我在视图背后的代码中将ViewModel定义并初始化为属性时, NavigationService为null。

Solution is to define ViewModel in xaml ( <Page.DataContext> ) then everything works like a charm. 解决方案是在xaml( <Page.DataContext> )中定义ViewModel,然后一切都像一个<Page.DataContext>一样工作。

对于任何回到此问题的人,我都遇到了问题,并意识到,如果我保留了项目10586的最低版本,问题就会消失。

I had the same issue when I updated the Target min version of the project to Fall Creators Update (see project properties ). 将项目的目标最低版本更新为Fall Creators Update时,我遇到了相同的问题(请参阅project properties )。

After reading this thread I fixed the issue by updating the Newtonsoft.Json Nuget dependency from version 11.xx to 10.0.3 . 阅读线程后,我通过将Newtonsoft.Json Nuget依赖项从11.xx版本11.xx10.0.3解决了该问题。

So just to be clear I downgraded the version of the Newtonsoft.Json Nuget dependency. 为了清楚起见,我降级了Newtonsoft.Json Nuget依赖项的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM