如何使用 Unity 和 MVVM 在 WPF 应用程序中切换视图?
我对 WPF 非常陌生,正在尝试设置一个需要切换视图的应用程序。
例如,用户正在查看包含多个组件的系统,当他们单击某个组件时,应该切换到与该组件关联的视图,替换以前的系统视图。我的理解是这里应该使用控制器,但实现却让我困惑。
我找到了一些示例,但这些项目对我来说有点太大了,无法真正跟踪视图切换的具体情况。这里真正对我有帮助的是应用程序文件中 Unity 设置中允许多个视图的一些示例代码、控制器中用于切换视图的代码以及与使控制器切换视图的按钮关联的代码。
谢谢
I am very new to WPF and am trying to set up an application that requires switching of views.
For example, a user is viewing a system that contains a number of components, when they click on a component, it should switch to a view that is associated to that component, replacing the previous system view. It's my understanding a Controller should be used here but the implementation eludes me.
I have found a few examples, but the projects are a bit too large for me to actually follow what is going on specifically with the view switching. What would really help me here is some example code from the Unity setup in the App file that allows multiple views, the Code in the controller that switches the view, and the code associated with a button that makes the controller switch the view.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您刚刚开始使用 WPF 并直接开始使用 IoC/MVVM,那么您可能需要考虑退一步并从 WPF 基础知识开始,即布局、路由事件、命令、绑定、依赖属性、INotifyPropertyChanged 等...
为了帮助您入门:wpf 和 mvvm 教程。
对于我们大多数凡人来说,WPF 的学习曲线很陡峭。然而,一旦你克服了第一个困难,“顿悟时刻”就会开始定期出现。
If you are just beginning with WPF and diving directly into using IoC/MVVM, then you may want to consider taking a step back and starting with the WPF fundamentals, i.e., layouts, routed events, commanding, binding, dependency properties, INotifyPropertyChanged, etc...
To get you started: tutorials on wpf and mvvm.
For most of us mere mortals, WPF has a steep learning curve. Yet, once you make it over that first hump, the 'aha moments' start kicking in on a regular basis.
我正在使用 Mvvm-Light,但我相信 Unity 也会类似。
您应该有一个 ViewModelLocator,用于注册 ViewModel。
您应该在某个地方有样式或数据模板,告诉框架根据遇到的视图模型显示什么视图。
您应该绑定一些属性,即基本视图模型。
从那里,您所要做的就是将该属性更改为不同的视图模型,并且您的视图将相应更新。
正如 Metro 所说,学习曲线陡峭,但一旦你习惯了,它就开始有意义了:)
I'm using Mvvm-Light, but I believe Unity will be similar.
You should have a ViewModelLocator where you register ViewModels.
You should have somewhere styles or datatemplates that tell the framework what view to show depending on the view model encountered.
You should have some property you bind to, that is a base view model.
From there, all you'll have to do is change that property to a different view model, and your view will update accordingly.
As Metro said, steep learning curve, but once you get used to it, it starts to make sense :)