簡體   English   中英

Template10 NavigationService為空

[英]Template10 NavigationService is null

這是我的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;
    }
}

每次啟動該應用程序時,都會出現以下異常:

System.NullReferenceException verificata HResult = -2147467261消息=對象引用未設置為對象的實例。

NavigationService始終為null。

我該如何解決?

我遇到過類似的問題,但是在ViewModel內部不在App.xaml.cs中。

因此,當我在視圖背后的代碼中將ViewModel定義並初始化為屬性時, NavigationService為null。

解決方案是在xaml( <Page.DataContext> )中定義ViewModel,然后一切都像一個<Page.DataContext>一樣工作。

對於任何回到此問題的人,我都遇到了問題,並意識到,如果我保留了項目10586的最低版本,問題就會消失。

將項目的目標最低版本更新為Fall Creators Update時,我遇到了相同的問題(請參閱project properties )。

閱讀線程后,我通過將Newtonsoft.Json Nuget依賴項從11.xx版本11.xx10.0.3解決了該問題。

為了清楚起見,我降級了Newtonsoft.Json Nuget依賴項的版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM