开发者

Should I be using a Page, Window or UserControl

开发者 https://www.devze.com 2023-02-11 19:15 出处:网络
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 w

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.

开发者_如何学Python

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

Thanks


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

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>

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

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>

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

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

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


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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消