UWP Open New Window in Class

1.6k views Asked by At

There are too many pages in my project and I want to write the opening and closing of these pages in one class. But it does not open to a new page.

Thank you.

My Class Code

class Class
{
    public static void GoToOpenPage1()
    {
        Frame OpenPage1 = new Frame();
        OpenPage1.Navigate(typeof(MainPage));
    }
}

Button Click Code

private void button_Click(object sender, RoutedEventArgs e)
    {
        Class.GoToOpenPage1();
    }
2

There are 2 answers

0
Richard Zhang On BEST ANSWER

After you create a Frame, it needs to be inserted into the current visual tree so that it can be "see".

For example, we create a Grid in MainPage.xaml.

<Grid x:Name="FrameContainer"  x:FieldModifier="public">

</Grid>

In MainPage.xaml.cs, we can expose the MainPage instance through static variables.

public static MainPage Current;
public MainPage()
{
    this.InitializeComponent();
    Current = this;
}

In this way, when MainPage is loaded, FrameContainer will also be loaded. We can get this Grid externally through MainPage.Current.FrameContainer, and then insert the Frame we generated into this Grid, which completes the insertion into the visual tree step.

public static void GoToOpenPage1()
{
    Frame OpenPage1 = new Frame();
    OpenPage1.Navigate(typeof(OtherPage));
    MainPage.Current.FrameContainer.Children.Clear();
    MainPage.Current.FrameContainer.Children.Add(OpenPage1);
}

But judging from the code you provided, you seem to be navigating to MainPage. If you need to make MainPage become the content of the Window again, you can write like this:

var rootFrame = Window.Current.Content as Frame;
rootFrame.Navigate(typeof(MainPage));

The above content belongs to page navigation, if you want to open a new window, you can refer to this document, which provides detailed instructions:

0
mm8 On

If you target Windows 10 version 1903 (SDK 18362) or later, you could open a window using the AppWindow API:

class Class
{
    public static async Task GoToOpenPage1()
    {
        AppWindow appWindow = await AppWindow.TryCreateAsync();
        Frame OpenPage1 = new Frame();
        OpenPage1.Navigate(typeof(MainPage));
        ElementCompositionPreview.SetAppWindowContent(appWindow, OpenPage1);
        await appWindow.TryShowAsync();
    }
}

On earlier versions, you should create a CoreApplicationView:

class Class
{
    public static async Task GoToOpenPage1()
    {
        CoreApplicationView newView = CoreApplication.CreateNewView();
        int newViewId = 0;
        await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            Frame OpenPage1 = new Frame();
            OpenPage1.Navigate(typeof(MainPage));
            Window.Current.Content = OpenPage1;
            Window.Current.Activate();
            newViewId = ApplicationView.GetForCurrentView().Id;
        });
        bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
    }
}