How to properly switch the MainPage? I have tried two methods unsuccessfully.

I have tried to different methods to get my MainPage to change and it is not happening. Basically when my App starts it needs to do some Async tasks to initialize everything, and then get the real main page to display. I first tried an event handler in App.cs. I can confirm the event handler does fire off in my debugger. However the code to switch the main page is not successfully doing so. Nothing changes in my UI. This code is below:

private void UnitStatusModelChanged(object sender, EventArgs e)
    UnitStatusModel = _unitStatusMonitor.UnitStatusModel;
    UnitStatusViewModel = new UnitStatusViewModel(UnitStatusModel, InputResourceHandler);
    MainPage = new MainTabbed(UnitStatusViewModel, InputResourceHandler);
    Debug.WriteLine("Switched page to " + UnitStatusModel.Version.Name);

protected override void OnStart()
    _unitStatusMonitor.UnitStatusChanged += new UnitStatusModelChangedEventHandler(UnitStatusModelChanged);

I thought maybe setting MainPage this way is not the way to go. So I tried making the MainPage a blank NavigationPage, and then pushing a model page on top once the app is ready. This also had no effect.

        var newPage = new MainTabbed(
            new UnitStatusViewModel(_unitStatusModel, inputResourceHandler),

I can confirm that if I start the app with a MainTabbed page, the page does display, so it should not be a problem with MainTabbed. Again, I also verified the in the debugger these lines of code are being executed with no exceptions being thrown. Still, no change to MainPage. What gives?

Best Answer


  • JordanRestifoJordanRestifo USMember

    Something is definitely silently dieing. I cannot get DisplayAlert to work. I cannot set to a blank page. Lastly, I tried to App.Current.MainPage.Navigation.PushModalAsync in a "StartPage" and that causes an unhandled null reference exception. Any tips?

Sign In or Register to comment.