Access custom application settings in WPF Prism 4

I developed the Microsoft Prism 4.0 application, and I'm trying my best to figure out how to make modules available for custom applications.

eg. Properties .Settings.Default.MyProperty.

I can define a simple class that is populated through the loader (which has a property descriptor) and injects it into the modules, but if I want to save the property change, I need to believe that I need a handle for the Properties.Settings context, which is not available for my modules ( which are simple class libraries).

+4
source share
1 answer

Your script sounds like the perfect reason to use the Service class. Create an interface and class called IUserSettingsService and UserSettingsService that has information (or can load and save it where it is stored).

Read chapter 3 of the Prism 4.0 help file, “Managing Dependencies Between Components,” “Registering Types” with (Unity or MEF, depending on what you use as the DI container).

Then, in your ViewModel, which requires custom settings, find and use your service. In MEF, it is as simple as adding a property of type IUserSettingsService with an attribute [Import] or using [ImportingConstructor] and with a parameter of type IUserSettingsService. With Unity, you get access to the service through the container. See Chapter 10 of the Prism Help File - Advanced MVVM Scripts.

Added after discussing comments: Since you need your service to be in the context of your main application, you can implement your class of service there. Create an interface for your service in a shared library that can be used by both the main project and your modules. Finally, load the service into the loader class, not the module.

+4
source

All Articles