Window VS Page VS Usercontrol for Wpf Navigation

UserControl VS Page in WPF

Navigation can be succefully implemented by using Frame/Pages or ContentControl/Views. It is a matter of choice.

However, Frame/Page have some gotchas, e.g. page.DataContext not inherited from parent Frame?

If you don't need isolation specifically, then stick to ContentControl. Navigation in prism framework is built with regions which are located in different type of controls (e.g. ContentControl, TabControl), not Frame (see docs)


one more approach for simple navigation is ViewModel based.

Examples:

WPF MVVM navigate views

Navigation with MVVM by Rachel Lim (external)

Should I be using a Page, Window or UserControl

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.

Window vs User Control

We make user control if we want to reuse it. As name says User Control it means some control like grid,combo box like that.If i need same grid on 3-4 windows then i will prefer to make it as User Control.If it is not reusable i will define my grid in the required window.At last you paste your user control on some window.

Conclusion :- If you want to reuse the control then make it as a user control otherwise define it in required window.

When should I use a UserControl instead of a Page?

"NavigationWindow does not store an
instance of a content object in
navigation history. Instead,
NavigationWindow creates a new
instance of the content object each
time it is navigated to by using
navigation history. This behavior is
designed to avoid excessive memory
consumption when large numbers and
large pieces of content are being
navigated to. Consequently, the state
of the content is not remembered from
one navigation to the next. However,
WPF provides several techniques by
which you can store a piece of state
for a piece of content in navigation
history...."

http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationwindow.aspx

Page vs Window in WPF?

Pages are intended for use in Navigation applications (usually with Back and Forward buttons, e.g. Internet Explorer). Pages must be hosted in a NavigationWindow or a Frame

Windows are just normal WPF application Windows, but can host Pages via a Frame container

User Control vs Pages vs Window on WPF Browser Applications

I use Pages in XBAPs the exact same way I would use a Window object in WPF. That is to say, rarely.

I usually have one Page/Window for my application and that is it. Switching the current view is usually done by switching a CurrentView property in my ApplicationViewModel, which changes what View is displayed in the main page.

I use UserControls when I want to create some kind of generic control, or for my Views. My Views can also be DataTemplates, and it is not uncommon for me to have a UserControl View that also has other Views in the UserControl.Resources (providing that all Views are related)

For example, I might have a UserControl called ProductsView which is the View that displays a list of Product objects, and the UserControl.Resources will contain a DataTemplate called ProductView which defines how WPF should display the ProductModel.

Universal app difference between page and usercontrol, and how to create static regions

In WPF we have window or usercontrol, and these are clear to me,
however in universal app there is a page and usercontrol as well

The WPF XAML Framework and UWP XAML Framework have some different design.

WPF:

public class Window : ContentControl, IWindowService

UWP:

public class Frame : ContentControl, IFrame, INavigate, IFrame2, IFrame3
public class Page : UserControl, IPage, IPageOverrides

Frame vs. Window

WPF is a Windows desktop application, so it uses the Window as the root element.
UWP introduced a build-in navigation model, the page is in a frame and the frame has the ability to navigate between pages.

So let’s say I have a header and a footer section, I want them to be consistant as the dont change.

You can put the navigable content in a sub-frame.

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<TextBlock>Header</TextBlock>
<Frame x:Name="pages">
<TextBlock>Content</TextBlock>
</Frame>
<TextBlock>Footer</TextBlock>
</StackPanel>
</Grid>

Sample Image

How to navigate from a main window to other user control without using mvvm in wpf?

Seems that question you asked is not complete, but I'll try to answer.

So you have window, and some content inside of it, and that's MainWindow i suppose. You want to change content of main window to contain your user control which is also defined in separate file. So, you can't do this in the way you described.

UserControl is only a control that window can contain, so basicaly you need something like placeholder in your window to change content dynamically like web browser navigation, and that's what Frame is.

Frame is control that helps to provide navigation between pages. You can create Frame inside your MainWindow and also create some pages. Then you will be able to implement switching between pages in your frame using Frame.Navigate() method.

Here is similar question described, please take look:

Window vs Page vs UserControl for WPF navigation?

Also there is a good article that may help you:

http://paulstovell.com/blog/wpf-navigation



Related Topics



Leave a reply



Submit