wpf 我应该使用页面、窗口还是用户控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5025504/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:35:14  来源:igfitidea点击:

Should I be using a Page, Window or UserControl

wpfxaml

提问by n00b

I'm developing a new desktop application that will have several views such as a dashboard, event viewer, chart viewer to name a few. Essentially the user will switch between one of these view which will cover the whole screen and not just a part of it.

我正在开发一个新的桌面应用程序,它将具有多个视图,例如仪表板、事件查看器、图表查看器等等。本质上,用户将在这些视图之一之间切换,该视图将覆盖整个屏幕,而不仅仅是其中的一部分。

I'm currently stuck on whether I should be creating a new Window, Page or UserControl for each dashboard, event viewer, chart viewer etc.

我目前纠结于是否应该为每个仪表板、事件查看器、图表查看器等创建一个新的窗口、页面或用户控件。

I have done some reading and understand that Pages were built for navigation which in turn lets me keep a history of the navigation so I can go back/forward. However I don't think I need that functionality for my desktop application.

我已经阅读并了解页面是为导航而构建的,这反过来又让我保留了导航的历史记录,以便我可以返回/前进。但是,我认为我的桌面应用程序不需要该功能。

So can I use either a UserControl or a Window? Or should there only be one Window per application?

那么我可以使用 UserControl 或 Window 吗?或者每个应用程序应该只有一个窗口?

Thanks

谢谢

回答by kiwipom

A Window has things like Title bar (including min/max/close buttons, etc) and can be used to host XAML elements, such as User Controls.

窗口具有标题栏(包括最小/最大/关闭按钮等)之类的内容,可用于托管 XAML 元素,例如用户控件。

You are certainly not restricted to using one Window per Application, but some applications would choose that pattern (one window, hosting a variety of UserControls).

您当然不限于每个应用程序使用一个窗口,但有些应用程序会选择这种模式(一个窗口,托管各种用户控件)。

When you create a new WPF Application, by default your app is configured (in App.xaml) like this:

当您创建新的 WPF 应用程序时,默认情况下您的应用程序配置(在 App.xaml 中)如下:

<Application x:Class="WpfApplication1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>

The StartupUriproperty tells the app which Window to open first (you can configure this if you wish)

StartupUri属性告诉应用程序首先打开哪个窗口(您可以根据需要进行配置)

If you would like to logically separate your Window into pieces and do not want too much XAML in one file, you could do something like this:

如果您想在逻辑上将您的 Window 分成几部分,并且不想在一个文件中包含太多 XAML,您可以执行以下操作:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <local:HeaderUserControl Grid.Row="0" />
        <local:MainSectionUserControl Grid.Row="1" />
    </Grid>
</Window>

where HeaderUserControland MainSectionUserControlare UserControls encapsulating the aspects of that Window, as needed.

其中HeaderUserControlMainSectionUserControl被包封的UserControls该窗口的方面,如需要的。

If you want to show another Window, you can, in code, call Showor ShowDialogon an instance of the new Window you want to show...

如果要显示另一个窗口,则可以在代码中调用ShowShowDialog在要显示的新窗口的实例上...

Also - yes, a Page is part of a WPF Browser application, designed to be viewed in Internet Explorer.

此外 - 是的,页面是 WPF 浏览器应用程序的一部分,旨在在 Internet Explorer 中查看。

回答by Ken Wayne VanderLinde

A page is something you would use in a browser, not for a standalone application.

页面是您将在浏览器中使用的东西,而不是用于独立的应用程序。

The Window class represents a top-level object, that is, it is not meant to be contained in another control. All the windows you see while using the Windows OS (if they were WPF application) would be created by deriving from the Window class, and you would use the Window class to create your own windows.

Window 类表示顶级对象,也就是说,它不打算包含在另一个控件中。您在使用 Windows 操作系统时看到的所有窗口(如果它们是 WPF 应用程序)都将通过从 Window 类派生来创建,并且您将使用 Window 类来创建您自己的窗口。

The UserControl class lets you create new custom controls, in case a standard control does not already exist for what you need. A UserControl can be contained inside of a window or another control, but a Window is not contained inside anything (this is the big difference!)

UserControl 类允许您创建新的自定义控件,以防您需要的标准控件尚不存在。UserControl 可以包含在窗口或其他控件中,但 Window 不包含在任何东西中(这是最大的不同!)