MVVM How do I know how to distribute settings between my main view model (and other view models) and the settings dialog?

I am creating a settings dialog for my application, and now all the settings correspond to the settings of the main view model, but as I add more view and view models, some may not work.

I need to know what is best for loading the current settings in the settings dialog, and then saving the settings for the respective view models if the user clicks the OK button.

I will not use the Properties.Settings.Default system to save the settings, since I want my application to be as portable as possible, and this will save the user coverage settings in the directory: C:\Users\ username \Local Settings\Application Data\ ApplicationName Instead of my application directory.

If that matters, I use the Laurent Bugnion MVVM Light Toolkit.

+4
source share
3 answers

How about implementing this using messenger toolkit?

When changes are made to the ViewModel settings, you simply tell everyone who is interested in:

 Messenger.Send<Settings>(changedSettings); 

And all Viewmodels that need to know if the settings have been changed are logged in this message:

 Messenger.Register<Settings>(this, delegate(Settings changedSettings){loadSettings(changedSettings);}); 

Read here: Mvvm light messenger or check this similar post mvvm-light-how-to-access-property-in-other-view-model

+2
source

You can use MEF by exporting the settings view from each view model and importing them as a list of views that you add to the stack panel, or some of them in your main settings view.

A good source of information on using MEF is: http://mef.codeplex.com/wikipage?title=Guide

Here is an example of a program that I would like to get up earlier:

using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.Reflection;

 namespace zTestConsole { public interface ISimple { string Message { get; } } [Export("SimpleHello",typeof(ISimple))] public class SimpleHello : ISimple { [Export("Message")] public string Message { get { return "Silverlight rocks!"; } } } [Export("SimpleBello",typeof(ISimple))] public class SimpleBello : ISimple { [Export("Message")] public string Message { get { return "C# rocks!"; } } } public class SimpleMultiCat { [ImportMany("Message")] public IEnumerable<string> Messages { get; set; } } public class SimpleCat { [Import("SimpleHello")] public ISimple simple { get; set; } } class Program { private static CompositionContainer container; static void Main(string[] args) { AggregateCatalog catalog = new AggregateCatalog(); catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); SimpleMultiCat cats = new SimpleMultiCat(); SimpleCat cat = new SimpleCat(); Program.container = new CompositionContainer(catalog); try { Program.container.ComposeParts(cats); foreach (string message in cats.Messages) { Console.WriteLine(message); } } catch (CompositionException ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine(); try { container.ComposeParts(cat); Console.WriteLine(cat.simple.Message); } catch (CompositionException ex) { Console.WriteLine(ex.ToString()); } } } } 
+1
source

I also had this problem. The solution for me was to have something like an ISettingsService model. There would be 2 implementations. One for real service and one of taunts, which was used for design time and unit testing.

Example: http://compiledexperience.com/blog/posts/Blendable-MVVM-Dependency-Injection-and-Unit-Testing

+1
source

Source: https://habr.com/ru/post/1307903/


All Articles