Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7806217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:27:49+00:00 2026-06-02T02:27:49+00:00

I have two applications, one is the main application and the other is a

  • 0

I have two applications, one is the main application and the other is a designer form application.

At the moment I have my main application working with prism and mef. Some of the Views in my main application are just data entry forms. What I want from my designer form application is to load up a data entry form view so it can then be edited, but to do this I want to use a different viewmodel for designing purposes. I don’t want the form to attach to it’s usual viewmodel and try to get data etc.

How using MEF would I be able to provide a different export so that it picks this one up instead of the usual viewmodel? Ideally it would just replace the main application viewmodel, so it just uses that instead.

This is my example view, importing a viewmodel

[Export("PatientDetailView")]
[PartCreationPolicy(CreationPolicy.NonShared)]
public partial class PatientDetailView : UserControl
{
    [ImportingConstructor]
    public PatientDetailView(PatientDetailViewModel viewModel)
    {
        InitializeComponent();

        this.DataContext = viewModel;
    }
}

And here is the basis for my viewmodel:

[Export(typeof(PatientDetailViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class PatientDetailViewModel : ViewModelBase, IRegionManagerAware
{
    [ImportingConstructor]
    public PatientDetailViewModel(IEventAggregator eventAggregator, IDialogService dialogService, IRegionManager regionManager)
        : base(eventAggregator, dialogService, regionManager)
    {
       //Contains Commands etc for Saving Patient Detail Record
       //Receiving patient detail etc 
    }

}

UPDATE:

The above is contained in a patient module assembly. This works how it should for the main application. For the Designer application I want to replace the above view model with something like the below:

[Export(typeof(PatientDetailViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class PatientDetailViewModel : ViewModelBase, IRegionManagerAware
{
    [ImportingConstructor]
    public PatientDetailViewModel(IEventAggregator eventAggregator, IDialogService dialogService, IRegionManager regionManager)
        : base(eventAggregator, dialogService, regionManager)
    {
       //Contains Commands etc for Designing the form
       //No commands from the original VM so changes how it tries to work.
    }

}

The above is working to override the default behaviour for my main application. This VM will be contained in the Designer assembly or a separate designerVMs assembly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:27:52+00:00Added an answer on June 2, 2026 at 2:27 am

    since these are seperate applications, and a datacontext can be any object, the solution can be simple.

    The view is changed to import it’s datacontext by name.

    public static class MefContracts
    {
      public const string PatientDetailViewModel = "PatientDetailViewModel";
    }
    
    [Export("PatientDetailView")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public partial class PatientDetailView : UserControl, IPartImportsSatisfiedNotification
    {
      [Import( MefContracts.PatientDetailViewModel, typeof( object )]
      private object vm;
    
      public void OnImportsSatisfied()
      {
        this.DataContext = vm;
      }
    
    
      public PatientDetailView()
      {
        InitializeComponent();
      }
    }
    

    Then depending on the application you only include the ViewModel you want and export it by name

    [Export( MefContracts.PatientDetailViewModel, typeof( object ) ) )]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class PatientDetailViewModel : ViewModelBase, IRegionManagerAware
    {
      ....
    }
    

    update if you really cannot seperate assemblies (which is imo still the best option, there is nothing wrong with having the vm in a different assembly), you could work with a simple factory instead (to abstract the way a ViewModel is acquired): instead of importing the vm, you import a factory that can create the vm. Which one it creates is up to some configuration value that has to be set differently in each app. You will have to export the vms with a different contract then, else there is no (easy) way to differentiate between them. Example:

    public static class MefContracts
    {
      public const string PatientDetailViewModelMain = "PatientDetailViewModelMain";
      public const string PatientDetailViewModelDesigner = "PatientDetailViewModelDesigner";
    }
    
    //the main vm
    [Export( MefContracts.PatientDetailViewModelMain, typeof( object ) ) )]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class PatientDetailViewModel : ViewModelBase, IRegionManagerAware
    {
      ....
    }
    
    //the other vm
    [Export( MefContracts.PatientDetailViewModelDesigner, typeof( object ) ) )]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class OtherPatientDetailViewModel : ViewModelBase, IRegionManagerAware
    {
      ....
    }
    
    [Export("PatientDetailView")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public partial class PatientDetailView : UserControl
    {
      [ImportingConstructor]
      public PatientDetailView( PatientDetailViewModelFactory viewModelFactory )
      {
        InitializeComponent();
        this.DataContext = viewModelFactory.Create();
      }
    }
    
    [Export]
    class PatientDetailViewModelFactory
    {
      [Import]
      private CompositionContainer container{ get; set; }
    
      public enum AppType{ Main, Designer }
    
      public AppType AppType{ get; set; }
    
      public object Create()
      {
        return container.GetExportedValue<object>( 
          AppType == AppType.Main ? MefContracts.PatientDetailViewModelMain :
                                    MefContracts.PatientDetailViewModelDesigner );
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small workspace with 3 projects. One main (Swing) application, two other
I have one main application which launch two other process's, i just need to
I have an application that has two threads. The first one (the main thread)
I have two window form applications written in C, one holds a struct consisting
I have two web application hosted on different server.For eg. one is main application
I have two applications that need to talk to each other over HTTP. One
I have two applications in my project 'test' the applications are one.mxml and two.mxml
i have two flex applications and i want to pass the data from one
I have two web applications running in the same Tomcat Instance. In one of
I run foo.com. I have two different applications that live in foo.com: one is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.