我应该使用页面、窗口还是用户控件 [英] Should I be using a Page, Window or UserControl

查看:32
本文介绍了我应该使用页面、窗口还是用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

谢谢

推荐答案

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

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.

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

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).

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

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

<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>

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

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

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

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 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>

其中 HeaderUserControlMainSectionUserControl 是用户控件,根据需要封装了该窗口的各个方面.

where HeaderUserControl and MainSectionUserControl are UserControls encapsulating the aspects of that Window, as needed.

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

If you want to show another Window, you can, in code, call Show or ShowDialog on an instance of the new Window you want to show...

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

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

这篇关于我应该使用页面、窗口还是用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆