how to reset page in .NET 9.0

Bhuwan 756 Reputation points
2025-02-04T12:35:19.4733333+00:00

Previously, we were resetting like this:

void Reset()
{
    (Application.Current as App).MainPage.Dispatcher.Dispatch(() =>
    {
        (Application.Current as App).MainPage = new AppShell();
    });
}

How can we do the same thing in .NET 9.0, since in 9.0 we are using Windows and no longer have AppShell redirection?

protected override Window CreateWindow(IActivationState? activationState)
{
    return new Window(new AppShell());
}

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,887 questions
{count} vote

5 answers

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 47,931 Reputation points Microsoft Vendor
    2025-02-05T02:15:49.5633333+00:00

    Hello,

    You can replace Page by setting a static instance for Window.

    Please refer to the following code:

    public partial class App : Application
    {
        public static Window WindowInstance { get; private set; }
        public App()
        {
            InitializeComponent();
     
     
        }
        public static void Reset()
        {
            WindowInstance.Page = new MainPage();
        }
        protected override Window CreateWindow(IActivationState? activationState)
        {
            WindowInstance = new Window(new AppShell());
            return WindowInstance;
        }
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    You found this answer helpful.
    0 comments No comments

  2. Md Shahin 0 Reputation points
    2025-02-05T02:47:16.68+00:00

    void Reset()

    {

    (Application.Current as App).MainPage.Dispatcher.Dispatch(() =>
    
    {
    
        (Application.Current as App).MainPage = new AppShell();
    
    });
    

    }

    0 comments No comments

  3. Md Shahin 0 Reputation points
    2025-02-05T02:48:16.9366667+00:00

    void Reset()

    {

    (Application.Current as App).MainPage.Dispatcher.Dispatch(() =>
    
    {
    
        (Application.Current as App).MainPage = new AppShell();
    
    });
    

    }

    0 comments No comments

  4. Md Shahin 0 Reputation points
    2025-02-05T02:48:32.6066667+00:00

    void Reset()

    {

    (Application.Current as App).MainPage.Dispatcher.Dispatch(() =>
    
    {
    
        (Application.Current as App).MainPage = new AppShell();
    
    });
    

    }

    0 comments No comments

  5. Md Shahin 0 Reputation points
    2025-02-05T02:48:55.3966667+00:00

    void Reset()

    {

    (Application.Current as App).MainPage.Dispatcher.Dispatch(() =>
    
    {
    
        (Application.Current as App).MainPage = new AppShell();
    
    });
    

    }

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.