0
votes

I created a WPF MVVM app using MEF and Prism 5.0. I have MEF loading a module at startup called MobileRecruiterModule (below). I need to read some settings from App.config (or any config file really) and update the ViewModel properties with those config values so the View can pick them up.

Where is the appropriate place to load settings here? Do I do it in the MEF module (the thing that implements Microsoft.Practices.Prism.Modularity.IModule or in my View?

MobileRecruiterModule.cs

[ModuleExport(typeof (MobileRecruiterModule))]
public class MobileRecruiterModule : IModule
{
  /// <summary>
  /// The region manager
  /// </summary>
  [Import] public IRegionManager Region;

  /// <summary>
  ///     Notifies the module that it has be initialized.
  /// </summary>
  public void Initialize()
  {
      Region.RegisterViewWithRegion(RegionNames.MainContentRegion, typeof (MobileRecruiterView));
  }

  ...

}

MobileRecruiterView.xaml.cs

[Export("MobileRecruiterView")]
[PartCreationPolicy(CreationPolicy.Shared)]
[RegionMemberLifetime(KeepAlive = false)]
[Export]
public partial class MobileRecruiterView : UserControl
{
    [Import]
    public MobileRecruiterViewModel ViewModel
    {
        get { return (MobileRecruiterViewModel)DataContext; }
        set { DataContext = value; }
    }

    [ImportingConstructor]
    public MobileRecruiterView(MobileRecruiterViewModel vm)
    {
        InitializeComponent();
        DataContext = vm;
    }
}

MobileRecruiterViewModel.cs

[Export]
public class MobileRecruiterViewModel : BindableBase
{
    public string DatabaseServer { get; set; }

    ... and a few other properties that the XAML view binds to ...
}
1
Topic asks "How", but the body asks "Where". Which is right?stukselbax
Thanks for your attention to detail :) It's "Where".Mark Richman

1 Answers

1
votes

I would suggest that you should load your settings in ViewModel constructor. Because your ViewModel is the DataContext for the View, you have to initialize it before you show it. I hope that you do not store any BLOB in it, so the time for *.config loading will be small enough to do it on UI thread.