在 WPF 中区分窗口、页面和用户控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20292373/
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
Differentiate Window vs Page vs UserControl in WPF?
提问by Shivam Srivastava
I wondered if someone could help me. I'm new to WPF and am currently writing a desktop application, but I cannot seem to get my head around what to use when redirecting someone to a new section of the application.
我想知道是否有人可以帮助我。我是 WPF 的新手,目前正在编写一个桌面应用程序,但是在将某人重定向到应用程序的新部分时,我似乎无法弄清楚要使用什么。
My options appear to be
我的选择似乎是
Window
Page
UserControl
but I don't understand what the difference between them is, and when I should use each one.
但我不明白它们之间的区别是什么,以及何时应该使用它们。
Could someone explain the differences for me, and give an example of what situations/applications you may use each one for?
有人可以为我解释这些差异,并举例说明您可以将每种情况用于哪些情况/应用程序?
回答by Mike Perrenoud
I'm not sure there is a right answerhere, but let me try and guide you. The Windowclass was created to allow you to have a true window. So, if you need to open a dialog, put it in a Window. Generally you will have at least oneWindowto house the main form.
我不确定这里是否有正确的答案,但让我尝试指导您。该Window班的设立是为了让你有一个真正的窗户。所以,如果你需要打开一个对话框,把它放在一个Window. 通常,您将至少有一个Window来放置主窗体。
A Pagewas built to be used with the NavigationWindowclass. This allows you to build Pageclasses that are marked up like everything else, but just navigate to them under the covers. This is good if you have a single-paged application where the users just go back and forth between pages (i.e. a wizard).
APage是为了与NavigationWindow类一起使用而构建的。这允许您构建Page与其他所有内容一样标记的类,但只需在幕后导航到它们。如果您有一个单页应用程序,其中用户只是在页面之间来回切换(即向导),这很好。
A UserControlis a way to house reusable markup. These can be housed inside of any ContentControl. These can be swapped out of a "content pane" for example on the main window, like Outlook.
AUserControl是一种容纳可重用标记的方法。这些可以放在任何ContentControl. 这些可以从“内容窗格”中换出,例如在主窗口(如 Outlook)上。
Using that, I hope it helps guide you in the right direction on when to use which. They each have their own uses and are not necessarily exclusive.
使用它,我希望它有助于指导您在何时使用哪个的正确方向。它们每个都有自己的用途,不一定是排他性的。

