When should I use a UserControl instead of a Page?

前端 未结 4 1608
长发绾君心
长发绾君心 2021-01-12 03:27

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

相关标签:
4条回答
  • 2021-01-12 04:00

    "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

    0 讨论(0)
  • 2021-01-12 04:08

    Well, you're still going to use usercontrols to create reusable sub components, but as for app architecture, it comes down to use case really. If you're building a typical web application a Business/Navigation App should be fine. If you're writing a game, not so much. Likewise if you're doing something like an interactive advert or media player.

    0 讨论(0)
  • 2021-01-12 04:14

    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.

    0 讨论(0)
  • 2021-01-12 04:20

    I just found some other interesting information related to WPF NavigationWindow and Page on Paul Stovell's website.

    He has this to say about the NavigationWindow class:

    WPF includes a class called NavigationWindow, which is essentially a Window which also doubles as a Frame, by implementing most of the same interfaces. It sounds useful at first, but most of the time you need more control over the Window, so I've never had any need to use this class. I am just pointing it out for the sake of completeness, though your mileage may vary.

    See his in-depth article on WPF Navigation and the Magellan and WPF Page management issues he encountered when writing his Magellan WPF framework.

    0 讨论(0)
提交回复
热议问题