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();
}
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
.In
MainPage.xaml.cs
, we can expose the MainPage instance through static variables.In this way, when
MainPage
is loaded,FrameContainer
will also be loaded. We can get thisGrid
externally throughMainPage.Current.FrameContainer
, and then insert theFrame
we generated into thisGrid
, which completes the insertion into the visual tree step.But judging from the code you provided, you seem to be navigating to
MainPage
. If you need to makeMainPage
become the content of the Window again, you can write like this: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: