如何使用 WPF 中的代码将页面添加到框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/138096/
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
How to add a Page to Frame using code in WPF
提问by Robert H?glund
I have a Window where I have put a Frame. I would like to add a Page to the Frame when I click a button that is also on the Window but not in the Frame. There are several buttons in the Window and each click on a button should load a different Page in the Frame.
我有一个窗口,我在其中放置了一个框架。当我单击一个也在窗口上但不在框架中的按钮时,我想将一个页面添加到框架中。Window 中有多个按钮,每次单击按钮都会在 Frame 中加载不同的页面。
Since I'm a total newbie on this WPF stuff it's quite possible that this approach is not the best and I have thought about replacing the Frame with a Canvas and then make UserControls instead of Pages that will be added to the Canvas. I welcome any ideas and suggestions on how to best solve this.
由于我是这个 WPF 东西的完全新手,因此这种方法很可能不是最好的,我曾考虑用 Canvas 替换 Frame,然后制作 UserControls 而不是将添加到 Canvas 的 Pages。我欢迎任何关于如何最好地解决这个问题的想法和建议。
I aiming for a functionality that is similar to the application Billy Hollis demonstrated in dnrtv episode 115. (http://dnrtv.com/default.aspx?showID=115).
我的目标是类似于 dnrtv 第 115 集展示的应用程序 Billy Hollis 的功能。(http://dnrtv.com/default.aspx?showID=115)。
回答by Joachim Kerschbaumer
the Frame class exposes a method named "Navigate" that takes the content you want to show in your frame as parameter. try calling
Frame 类公开了一个名为“Navigate”的方法,该方法将您要在框架中显示的内容作为参数。试着打电话
myFrame.Navigate(myPageObject);
this should work
这应该有效
回答by BASIR ALMAS
yourFramName.NavigationService.Navigate(yourPageObject)
yourFramName.NavigationService.Navigate(yourPageObject)
e.g Frame1.NavigationService.Navigate(new Page1());
例如 Frame1.NavigationService.Navigate(new Page1());