简体   繁体   中英

Template10 NavigationService is null

This is my 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.

NavigationService is always null.

How can I solve this?

I have had similar issue, but inside of ViewModel not in App.xaml.cs.

So NavigationService was null when I defined and initialized my ViewModel as property in code behind of view.

Solution is to define ViewModel in xaml ( <Page.DataContext> ) then everything works like a charm.

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

I had the same issue when I updated the Target min version of the project to Fall Creators Update (see 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 .

So just to be clear I downgraded the version of the Newtonsoft.Json Nuget dependency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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