新 WPF 项目的 Prism 和 MVVM
我很快就会开始一个新项目,并正在向那些有 WPF、Prism 和 MVVM 经验的人寻求一些架构建议。
该项目肯定是 WPF,我将实现 MVVM(我可能会使用 Josh Smith 的 MVVM 基金会作为起点),以便能够从 UI/逻辑分离等中受益。但我不确定我是否会这样做使用 Prism 来构建我的项目也能受益匪浅。
让我简单介绍一下这个项目。将有一个主“工具栏”,其中显示许多小部件。每个小部件都会显示一些与其功能相关的基本数据,单击该小部件将打开一个新窗口,该窗口将显示更详细的数据,并包含用于查看/编辑数据的丰富 UI。
现在,我想我可以使用 Prism 来构建该项目,但我以前从未使用过它,并且不确定它是否适合我想要实现的目标。例如,我的“工具栏”是否是一个包含每个小部件将填充的区域的外壳?单击小部件时显示的每个新窗口是否也是具有自己的区域设置的自己的外壳?如果我可以获取工具栏和工具栏上的一个小部件的模式,我就可以为其余小部件复制它。
除了 Prism 之外,我还有一个关于如何针对某些数据编辑窗口实现 MVVM 的问题。假设我有一个显示一些数据的图表,用户可以直接单击/鼠标在图表上移动来编辑他看到的数据。所有数据都在模型中,视图模型通过绑定使数据可供视图使用。我的问题是,特定于该视图中图表的鼠标单击/移动事件将写入哪里?我们不想在视图的代码后面有太多/任何东西,并且我们不想在视图模型中包含 UI 事件处理程序,所以我不确定如何处理这种类型的场景。我知道命令是可能的答案,但我见过的 MVVM 示例通常显示简单按钮单击的示例命令。总体思路是一样的吗?
因此,如果有人对上述内容有任何建议或有关使用 WPF 和 MVVM/Prism 的任何一般提示,请告诉我。
谢谢。
I will be starting a new project soon and am looking for some architectural advice from those of you who have experience with WPF, Prism, and MVVM.
The project will definitely be WPF and I will be implementing MVVM (I will likely use Josh Smith's MVVM Foundation as a starting point) in order to be able to benefit from the separation of UI/logic etc. I am not sure though if I would benefit from using Prism as well to structure my project.
Let me briefly describe the project. There will be a main "toolbar" that will display a number of widgets. Each widget displays some basic data related to its function and clicking the widget will open a new window that will display much more detailed data and contain a rich UI for viewing/editing the data.
Now, I was thinking that I can use Prism to frame the project but I have never used it before and am not sure if it is suitable for what I am trying to achieve. For example, would my "toolbar" be a shell that contains regions that each widget would populate? Would each new window that is displayed when a widget is clicked also be its own shell with its own region setup? If I can get the pattern down for the toolbar and one widget on the toolbar, I can replicate it for the rest of the widgets.
Aside from Prism, I have a question about how MVVM should be implemented for certain data editing windows. Let's say I have a chart that displays some data and the user is able to directly click/mouse move on the chart to edit the data that he sees. All of the data is in the model and the view model is making that data available to the view via binding. My question is where will the mouse click/move events, that are specific to the chart in that view, be written? We don't want much/anything in the view's code behind and we don't want to have UI event handlers in the view model so I am not sure how this type of scenario is handled. I know that commands are the likely answer here but the MVVM samples I have seen usually show sample commands for simple button clicks. Is the general idea the same?
So, if anyone has any suggestions on the above or any general tips on working with WPF and MVVM/Prism, please let me know.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那里有一些问题,所以我会尽力回答所有问题。
我参与的项目包含 WPF、MVVM 和 Prism 以及其他框架。最好的建议是在将它们粘合在一起之前先了解它们的威力和功能。您不必使用 Prism 的所有功能即可在这种情况下发挥作用。
对于 Prism,您可以使用...
上述 4 个并非都必须使用,但可以轻松集成到 MVVM /WPF 应用程序中。
您可以在此处创建一个区域(可以从区域派生)来管理工具栏上的按钮。 (我使用了关于功能区的区域)。服务可以通过一个接口公开,每个模块都可以提供命令/图像(无论你有什么),当单击它时将创建一个 ViewModel。您可以在模块的初始化中执行此操作。
如果每个按钮都会打开一个全新的窗口,我建议引入一个通用控制器类,该类将创建一个通用窗口并附加一个您的模块创建的视图模型。在这种情况下,没有真正需要使用区域,除非您将不同的视图粘合到一个应用程序窗口,该窗口保持打开状态的时间比视图本身的生命周期长。基本形式的窗口可以简单地是这样的......
在你的控制器中它可以做到这一点......
控制器可以在你的模块中直接使用或包装在服务中......尽管为了可测试性服务和接口是最好的。确保您已将模块资源与
Applicaiton.Resources
合并,并使用DataTemplate
将视图链接到视图模型。不要害怕代码隐藏,但在这种情况下,您可以使用 EventToCommand 附加行为来路由到命令在你的视图模型上。 MVVMLight 工具包具有此功能,您可以根据需要重复使用。
DI 非常强大,即使没有 Prism,我也鼓励使用它,因为构建视图模型会更容易。
华泰
There are a few questions in there so I will do my best at covering them all.
I worked on a project that had WPF, MVVM, and Prism along side other frameworks. The best advice is to understand the power and functionality of each before glueing it all together. You don't have to use all the features of Prism for it to be useful in this situation.
For Prism you can use...
The above 4 don't all have to be used but can easily be integrated in a MVVM /WPF application.
Here you can have a region you create (you can derive from Region) that will manage the buttons on the toolbar. (I have used a region with regards to a Ribbon). A service can be exposed via an interface that each module can supply the command/image (what ever you have) that when it is clicked will create a ViewModel. You can do this inside the module's Initialisation.
If each button opens a brand new window I would suggest introducing a common controller class that will create a generic use window and attach a view model that your module creates. No real need to use regions in this case unless you are gluing different views to a application window that stays open longer than the life of the view itself. The window in basic form can simply be this...
Where within your controller it can do this...
The controller can be used within your module directly of wrapped within a service... although for testabilty a service and interface would be best. Make sure you have merged your module resources with the
Applicaiton.Resources
and useDataTemplate
's to link your view to the view model.Don't be afraid of code behind but you can in this case use EventToCommand attached behaviour that will route to a command on your viewmodel. MVVMLight toolkit has this which you can reuse if you want.
DI is very powerful and I encourage using it even without Prism as constructing your view models will be easier.
HTH
我认为 Prism 非常适合您。
-> 我的“工具栏”是一个包含每个小部件将填充的区域的外壳吗?
这样做的最大优点是,如果您添加更多模块,则无需更改任何内容。
-> 单击小部件时显示的每个新窗口是否也是具有自己的区域设置的自己的外壳?
不,您可以在 shell 中使用“WindowRegionAdapter”在单独的窗口中为您的小部件创建视图。
-> 特定于该视图中图表的鼠标单击/移动事件将写入哪里?
您可以纯粹使用 XAML 使用附加行为将视图中的事件绑定到 ViewModel 中的命令。谷歌“混合行为”或“附加行为”,了解如何做到这一点。无需为此编写任何代码。
老实说,我只是想为您提供您想要搜索的关键字,以获取您需要的所有信息。
I think Prism will work great for you.
->would my "toolbar" be a shell that contains regions that each widget would populate?
The biggest advantage with this is that if you add more modules you don't need to change anything.
->Would each new window that is displayed when a widget is clicked also be its own shell with its own region setup?
No, you can use a 'WindowRegionAdapter' in the shell to create views for your widgets in separate windows.
->where will the mouse click/move events, that are specific to the chart in that view, be written?
You can use attached behaviors to bind events in your view to commands in the ViewModel purely in XAML. Google 'Blend behaviors' or 'attached bahaviors' for how you could go about doing it. There is no need to write any code behind for this.
To be honest I am only trying to give you the keywords you'd want to search to get all the information you need.