Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use a UserControl instead of a Page?

I notice that many of the WPF MVVM frameworks seem to avoid using the NavigationWindow and Page controls in favor of composing pages using nested UserControls.

The NavigationWindow and Page provide easy ways to enable back and forward navigation in the journal as well as providing an easy way to pass data among pages. Most MVVM frameworks I've seen re-implement these features in various ways.

Is there a specific reason to avoid using NavigationWindow and Page?

like image 589
dthrasher Avatar asked Apr 12 '10 13:04

dthrasher


People also ask

Should I use usercontrols in Windows or pages?

Use Windows if you're building a dialog based app. Use Pages if you're building a navigation based app. UserControls will be useful regardless of the direction you go as you can use them in both Windows and Pages.

What is the base of a usercontrol?

The base " UserControl " is nothing but a Custom Control that you derive to create a control UI specific to your project. Generally, we create a UserControl which is placed inside an XAML page with tight bonding to the code behind.

What is the use of usercontrol in XBAP?

It is mostly used for web-based systems like an XBAP, where you have a single browser window and different pages can be hosted in that window. It can also be used in Navigation Applications like sellmeadog said. A UserControl is a reusable user-created control that you can add to your UI the same way you would add any other control.

What is usercontrol in Salesforce?

The base " UserControl " is nothing but a Custom Control that you derive to create a control UI specific to your project. Generally, we create a UserControl which is placed inside an XAML page with tight bonding to the code behind. You can directly access its UI elements from the code-behind and do some specific operations.


2 Answers

"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

like image 131
jasonk Avatar answered Sep 25 '22 06:09

jasonk


I just discovered another difference between UserControls and Pages: Pages cannot be used as DataTemplates.

For example, if you were creating application using the MVVM style, you might expect this to work:

    <DataTemplate DataType="{x:Type ViewModels:ProjectDashboardViewModel}">
        <Views:ProjectDashboardView />
    </DataTemplate>

But if the ProjectDashboardView is a Page, it will fail.

like image 34
dthrasher Avatar answered Sep 26 '22 06:09

dthrasher