bodangly
bodangly

Reputation: 2624

Cannot get MainPage in Xamarin.Forms to react/change

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);
        _commandQueue.StartQueue();
    }

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),
                inputResourceHandler
                );
            App.Current.MainPage.Navigation.PushModalAsync(newPage);

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?

Upvotes: 0

Views: 601

Answers (1)

joe
joe

Reputation: 1125

Check your thread after your async completes, when you call PushModalAsync (or any UI thing). Make sure it's on the main thread.

Upvotes: 1

Related Questions