WPF:具有页面导航的复合应用程序
我目前正在编写一个应用程序,复合方法就像手套一样适合......几乎!
我还需要一种在视图之间导航的方法,包括维护用于向后和向前导航的日志。
结合这两种方法的最佳方式是什么?一方面是基于单一 Window
的 CAG shell 及其 UserControl
派生视图,另一方面是方便的 NavigationWindow
shell 及其 Page
派生视图和日志?
谢谢!
I am currently writing an application to which the composite methodology fits like a glove.... almost!
I also need a way to navigate between views, including maintaining a journal for navigation backward and forward.
What is the best way to combine these two methodologies, on one hand the single Window
based CAG shell with its UserControl
derived views, and on the other hand the convenient NavigationWindow
shell with its Page
derived views and journal?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在
NavigationWindow
中显示任何内容,而不仅仅是Pages
。使其工作的一个简单方法是在NavigationWindow
的资源中为要显示的每个 ViewModel 定义一个DataTemplate
。将NavigationWindow
的Content
属性绑定到主 ViewModel 的属性,就完成了:更改该属性将更新NavigationWindow
内容,并且将自动选择适当的DataTemplate
更新
我刚刚查看了我使用
NavigationWindow
的项目的代码。实际上我错了,通过绑定Content
不起作用(或者也许它有效,但这不是我所做的)。相反,我创建了一个INavigationService
接口,由我的App
类实现,该类通过调用NavigationWindow.Navigate
方法来处理导航。这样,导航历史记录就由NavigationWindow
维护。的摘录
这是我的项目MainWindow.xaml:
App.xaml.cs
:当我需要导航到另一个视图时,我只需调用
Navigate
方法以 ViewModel 作为参数,WPF 会自动从资源中选择适当的DataTemplate
。You can display anything in a
NavigationWindow
, not justPages
. A simple way to make it work is to define in theNavigationWindow
's resources aDataTemplate
for each ViewModel you want to display. Bind theContent
property of theNavigationWindow
to a property of your main ViewModel, and you're done : changing that property will update theNavigationWindow
content, and the appropriateDataTemplate
will be picked automaticallyUPDATE
I just looked at the code of a project of mine where I used a
NavigationWindow
. Actually I was mistaken, it doesn't work by binding theContent
(or maybe it works, but that's not what I did). Instead I created aINavigationService
interface, implemented by myApp
class, which handles the navigation by calling theNavigationWindow.Navigate
method. That way, the navigation history is maintained by theNavigationWindow
.Here's an extract from my project
MainWindow.xaml :
App.xaml.cs :
When I need to navigate to another view, I just call the
Navigate
method with the ViewModel as a parameter, and WPF automatically picks the appropriateDataTemplate
from the resources.