2017-09-26 102 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(); 
} 
相关问题