MVVM-我将如何在我的主视图模型(和其他视图模型)和我的设置对话框之间传播设置?
我正在为我的应用程序构建一个设置对话框,现在所有设置都与主视图模型上的设置相对应,但当我添加更多视图和视图模型时,有些可能不会。
我需要知道将当前设置加载到设置对话框中的最佳实践是什么,然后如果用户单击“确定”,则将设置保存到相应的视图模型中。
我不会使用 Properties.Settings.Default 系统来存储设置,因为我希望我的应用程序尽可能可移植,并且这会将用户范围的设置存储在目录中: C:\Users\ 用户名\Local Settings\Application Data\ ApplicationName
而不是在我的应用程序的目录中。
为了以防万一,我正在使用 Laurent Bugnion 的 MVVM Light Toolkit。
I am building a settings dialog for my application and right now all of the settings correspond with settings on the main view-model, but as I add more view's and view-models some may not.
I need to know what the best practice is for loading the current settings into the settings dialog and then saving the settings to thier corresponding view-models if the user clicks okay.
I will not be using the Properties.Settings.Default system to store settings since I want my application to be as portable as possible and this would store user scoped settings in the directory:C:\Users\ username \Local Settings\Application Data\ ApplicationName
Instead of in my application's directory.
In case it makes any difference I am using the MVVM Light Toolkit by Laurent Bugnion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用工具包的 Messenger 来实现怎么样?
当在设置 ViewModel 中进行更改时,您只需通知任何感兴趣的人:
所有需要知道设置是否已更改的 Viewmodel 都会注册该消息:
请阅读此处:Mvvm light Messenger 或查看类似的帖子 mvvm-light-how-to-access-property-in-other-view-model< /a>
How about implementing that with the Messenger of the toolkit?
When changes are made in the Settings ViewModel you just inform anyone interested:
And all Viewmodels which need to know if settings have been changed register to that message:
Have a read here: Mvvm light messenger or check this similar post mvvm-light-how-to-access-property-in-other-view-model
您可以使用 MEF,从每个视图模型中导出设置视图,并将它们作为视图列表导入,然后添加到堆栈面板或主设置视图中的某些此类视图中。
使用 MEF 的一个很好的信息来源是:http://mef.codeplex.com/wikipage? title=Guide
这是一个我打算早点起床的示例程序:
using System;
使用 System.Collections.Generic;
使用 System.ComponentModel.Composition;
使用 System.ComponentModel.Composition.Hosting;
使用系统反射;
You could use MEF, exporting a settings view from each view model and importing them as a list of views that you add to a stack panel or some such in your main settings view.
A good source of info of using MEF is: http://mef.codeplex.com/wikipage?title=Guide
Here is a sample program I meant to get up sooner:
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
我也遇到过这个问题。对我来说,解决方案是使用类似 ISettingsService 模型的东西。将有 2 个实现。一种用于实际服务,另一种用于设计时间和单元测试。
这里有一个例子:
http://compiledexperience.com/blog/posts/ Blendable-MVVM-依赖注入和单元测试
I've had this problem also. Solution for me was to have something like an ISettingsService Model. There would be 2 implementations. One for the real service and one mocked that was used for design time and unit testing.
An example here:
http://compiledexperience.com/blog/posts/Blendable-MVVM-Dependency-Injection-and-Unit-Testing