2016-09-13 66 views
0

這是我App.xaml.csTemplate10的NavigationService爲空

[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始終爲空。

我該如何解決這個問題?

+0

我用你的代碼測試過,它效果很好。你發佈的代碼是正確的。我認爲問題可能不在這裏。你能分享一個可以重現你的問題的[mcve]嗎?或者你可以用一個Minimal Template 10項目測試你的代碼,它應該能夠工作。 –

+0

如果我創建一個新項目,此代碼工作正常 – frenk91

回答

0

我有類似的問題,但ViewModel裏面沒有在App.xaml.cs.中。

因此,當我將ViewModel定義並初始化爲視圖後面代碼中的屬性時,NavigationService爲空。

解決方案是在xaml中定義ViewModel(<Page.DataContext>),然後所有東西都像魅力一樣。

相關問題