2017-09-26 104 views
0

我的主頁上的指令this.Frame.Navigate(typeof(RegionPage));不起作用。它會產生一個異常:第一次打開應用程序無法更改頁面

System.NullReferenceException:'未將對象引用設置爲對象的實例。'

所以我試圖把它放在mainpage後的一些函數中,一切正常。

我的目標是:我想製作一個控件,如果用戶第一次打開應用程序,它將顯示一個帶有教程的新頁面。

問題:我該如何解決該問題?

public MainPage() 
{ 
    this.InitializeComponent(); 

    Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested; 
    this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled; 

    TextBoxRicerca.Visibility = Visibility.Collapsed; 
    Mappe.Loaded += Mappe_Loaded; 

    Regione.RegNome = ""; 

    this.Frame.Navigate(typeof(RegionPage));     
} 
+0

這個異常說的是什麼? –

回答

0

我不喜歡使用延遲,並且在OnLaunched事件中編輯App.xaml.cs太困難了。 所以我做了一個混合,並把一個「Loaded + = Loading;」主,創造了..

public MainPage() 
{ 
    this.InitializeComponent(); 

    Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested; 
    this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled; 

    TextBoxRicerca.Visibility = Visibility.Collapsed; 
    Mappe.Loaded += Mappe_Loaded; 

    Regione.RegNome = ""; 

    **Loaded += Loading;** 

    //this.Frame.Navigate(typeof(RegionPage));     
} 

..created功能:

private void Loading(object sender, RoutedEventArgs e) 
     { 
      this.Frame.Navigate(typeof(RegionPage)); 
     } 

它只是給我,我要補充的消息「新」的地方不知道在哪裏和Don」知道爲什麼,但工作原理:d

「Avviso CS0108 'MainPage.Loading(對象,RoutedEventArgs)' nasconde IL membro ereditato 'FrameworkElement.Loading' 硒questo comportamentoèintenzionale,usare拉帕羅拉chiave新的。」

+0

它會要求您將'new'關鍵字添加到方法簽名中,因爲'Page'類已經有一個名爲'Loading'的成員(在FrameworkElement類中定義的一個事件),並且您的方法恰好具有相同的標識符。例如, 重構'Loading'方法以調用'Page_Loading',並且錯誤應該消失。 –

0

由於您的應用程序正在準備一些組件用於啓動,所以你需要給一些時間,您的應用程序加載組件。

所以,你需要給像this--

using System.Threading.Tasks; 


public MainPage() 
{ 
    this.InitializeComponent(); 
    Loaded += async (s, e) => 
    { 
     await Task.Delay(100); 
     Frame.Navigate(typeof(RegionPage)); 
    }; 
} 

您可以相應地調整延遲一些延遲。

人口統計學

Demo

備用Way-

及首次推出的,所以應該表現出一些特定的頁面或指南頁面,完整的解決方案,您可以編輯您應用.xaml.cs in OnLaunched event

using Windows.Storage; 


if (e.PrelaunchActivated == false) 
{ 
    if (rootFrame.Content == null) 
    { 
     IPropertySet roamingProperties = ApplicationData.Current.RoamingSettings.Values; 
     if (roamingProperties.ContainsKey("FirstTimePage")) 
     { 
      // regular visit 
      rootFrame.Navigate(typeof(MainPage), e.Arguments); 
     } 
     else 
     { 
      // first time visit 
      rootFrame.Navigate(typeof(RegionPage), e.Arguments); 
      roamingProperties["FirstTimePage"] = bool.TrueString;  
     } 
    } 
    // Ensure the current window is active 
    Window.Current.Activate(); 
} 
相關問題