在 WPF 中加载页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2547710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading page in WPF
提问by Shift
Guys, I have a basic WPF application. Contains App.xaml as always and a Mainwindow.xaml. I've created also some pages like page1/2/3. I want to load for example page1.xaml in mainwindow.xaml. Is this possible? And also want to close it so the content of mainwindow.xaml will stay in there.
伙计们,我有一个基本的 WPF 应用程序。一如既往地包含 App.xaml 和一个 Mainwindow.xaml。我还创建了一些页面,如 page1/2/3。我想在 mainwindow.xaml 中加载例如 page1.xaml。这可能吗?并且还想关闭它,以便 mainwindow.xaml 的内容留在那里。
I dont want this to be a navigation application with the left/right arrows at the top.
我不希望这是一个顶部带有左/右箭头的导航应用程序。
采纳答案by Maurizio Reginelli
You can add a frame to your main page and load the pages on it.
您可以向主页添加框架并在其上加载页面。
回答by astreal
I came here to add that there are many ways to load the pages into the frame:
我来这里补充一下,有很多方法可以将页面加载到框架中:
By setting the source (as @Shift mentioned)
通过设置源(如@Shift 所述)
frame1.Source = new Uri("Page1.xaml", UriKind.RelativeOrAbsolute);
By setting the Content:
通过设置内容:
frame1.Content= new Page1();
By using the NavigationService
:
通过使用NavigationService
:
frame1.NavigationService.Navigate(new Page1());
回答by Shift
Adding a frame and setting the source for the frame like makes my day :)
添加一个框架并设置框架的来源就像让我开心:)
frame1.Source = new Uri("Page1.xaml", UriKind.RelativeOrAbsolute);
回答by abhijithkp
Call below navigate function from inside any control or verification area.Page1 is the new page.So it opens like a new window from the application.
从任何控制或验证区域内部调用下面的导航功能。Page1 是新页面。因此它像应用程序中的新窗口一样打开。
NavigationWindow navigationWdw = new NavigationWindow();
navigationWdw.Height = this.Height;
navigationWdw.Width = this.Width;
navigationWdw.Show();
navigationWdw.Navigate(new Page1());