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?
Thanks
Window is the root control that must be used to hold/host other controls (e.g. Button) as container. Page is a control which can be hosted in other container controls like NavigationWindow or Frame. Page control has its own goal to serve like other controls (e.g. Button). Page is to create browser like applications.
A window is managed by the OS and is placed on the desktop. A UserControl is managed by wpf and is placed in a Window or in another UserControl. Applcations could be created by have a single Window and displaying lots of UserControls in that Window.
Content Control is a base class that provides standardised functionality to WPF Controls. The Content Control class represents controls that can include a single item of content. This content is commonly plain text or a child control. Content Control is a subclass of the Control class in WPF.
Step 1: Create an empty WPF using Visual Studio, enter the name for the application and click on OK. Step 2: Create a button using the following code or drag and drop a button from the ToolBox of Visual Studio 2013.
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!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With