0

The instruction this.Frame.Navigate(typeof(RegionPage)); on my mainpage doesn't work. It generates an exception:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

So I tried putting it in some function after the mainpage and all goes fine.

My objective: I want to make a control such that, if is it the first time that the user opens the app, it will display a new page with tutorial.

Question: how can I work around that problem?

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));                
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Uopo
  • 5
  • 2

2 Answers2

1

Due to your app is preparing some components for launching the so you need to give some time to your app to load components.

So you need to give some delay like this--

using System.Threading.Tasks;


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

you can adjust delay accordingly.

Demo-

Demo

Alternate Way-

And Full Solution for first time launch of your so it should show some specific page or tutorial page, you can edit your App.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();
}
Shubham Sahu
  • 1,963
  • 1
  • 17
  • 34
0

I don't like to use the delay, and editing App.xaml.cs in OnLaunched event was too difficult. So I made a mix and put a "Loaded += Loading;" on the main and created.

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));                
}

I created the function:

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

It's only giving me a message that I should add a "new" somewhere don't know where and don't know why, but works.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Uopo
  • 5
  • 2
  • It asks you to add the `new` keyword to the method signature because the `Page` class already has a member called `Loading` (an event defined on the `FrameworkElement` class), and your method happens to have the same identifier. Refactor the `Loading` method to be called `Page_Loading` for example, and the error should disappear. – ravindUwU Sep 30 '17 at 10:57