我什么时候应该使用UserControl而不是Page?

时间:2021-12-22 16:05:47

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.

我注意到许多WPF MVVM框架似乎避免使用NavigationWindow和Page控件来支持使用嵌套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.

NavigationWindow和Page提供了在日志中启用后退和前进导航的简便方法,以及提供在页面之间传递数据的简便方法。我见过的大多数MVVM框架都以各种方式重新实现这些功能。

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

是否有特定的理由避免使用NavigationWindow和Page?

4 个解决方案

#1


10  

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

“NavigationWindow不会在导航历史记录中存储内容对象的实例。相反,每次使用导航历史记录导航时,NavigationWindow都会创建内容对象的新实例。此行为旨在避免在大数字和因此,内容的状态不会从一个导航记忆到下一个导航。但是,WPF提供了几种技术,您可以通过这些技术为导航历史记录中的一段内容存储一段状态。 ......”

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

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

#2


3  

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

我刚发现UserControls和Pages之间的另一个区别:Pages不能用作DataTemplates。

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

例如,如果您使用MVVM样式创建应用程序,您可能希望这可以工作:

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

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

但是如果ProjectDashboardView是一个页面,它将失败。

#3


2  

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

我刚刚在Paul Stovell的网站上找到了一些与WPF NavigationWindow和Page相关的有趣信息。

He has this to say about the NavigationWindow class:

他对NavigationWindow类有这样的说法:

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.

WPF包含一个名为NavigationWindow的类,它实际上是一个Window,它也可以通过实现大多数相同的接口而兼作Frame。一开始听起来很有用,但大多数时候你需要对Window进行更多控制,所以我从来没有必要使用这个类。我只是为了完整性而指出它,尽管你的里程可能会有所不同。

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

请参阅他在撰写Magellan WPF框架时遇到的关于WPF Navigation以及他遇到的Magellan和WPF页面管理问题的深入文章。

#4


1  

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.

好吧,你仍然会使用usercontrols来创建可重用的子组件,但对于app架构,它归结为用例。如果您正在构建典型的Web应用程序,那么Business / Navigation App应该没问题。如果你正在写游戏,那就不是了。同样,如果您正在做类似互动广告或媒体播放器的事情。

#1


10  

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

“NavigationWindow不会在导航历史记录中存储内容对象的实例。相反,每次使用导航历史记录导航时,NavigationWindow都会创建内容对象的新实例。此行为旨在避免在大数字和因此,内容的状态不会从一个导航记忆到下一个导航。但是,WPF提供了几种技术,您可以通过这些技术为导航历史记录中的一段内容存储一段状态。 ......”

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

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

#2


3  

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

我刚发现UserControls和Pages之间的另一个区别:Pages不能用作DataTemplates。

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

例如,如果您使用MVVM样式创建应用程序,您可能希望这可以工作:

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

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

但是如果ProjectDashboardView是一个页面,它将失败。

#3


2  

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

我刚刚在Paul Stovell的网站上找到了一些与WPF NavigationWindow和Page相关的有趣信息。

He has this to say about the NavigationWindow class:

他对NavigationWindow类有这样的说法:

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.

WPF包含一个名为NavigationWindow的类,它实际上是一个Window,它也可以通过实现大多数相同的接口而兼作Frame。一开始听起来很有用,但大多数时候你需要对Window进行更多控制,所以我从来没有必要使用这个类。我只是为了完整性而指出它,尽管你的里程可能会有所不同。

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

请参阅他在撰写Magellan WPF框架时遇到的关于WPF Navigation以及他遇到的Magellan和WPF页面管理问题的深入文章。

#4


1  

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.

好吧,你仍然会使用usercontrols来创建可重用的子组件,但对于app架构,它归结为用例。如果您正在构建典型的Web应用程序,那么Business / Navigation App应该没问题。如果你正在写游戏,那就不是了。同样,如果您正在做类似互动广告或媒体播放器的事情。