简体   繁体   中英

Mapping derived ViewModels to base class View in Caliburn.Micro

I have a base ViewModel and associated View. I also have multiple derived ViewModels from the base ViewModel, but I'd like to use the base View for display.

Base ViewModel and View:

  • vm: MyCompany.MyApp.Modules.Wizard.ViewModels.WizardViewModel
  • vw: MyCompany.MyApp.Modules.Wizard.Views.WizardView

Derived from WizardViewModel :

  • vm: MyCompany.MyApp.Modules.NewSpec.ViewModels.NewSpecViewModel : WizardViewModel
  • vw: (map to MyCompany.MyApp.Modules.Wizard.Views.WizardView )

  • vm: MyCompany.MyApp.Modules.NewSpec.ViewModels.NewMaterialViewModel : WizardViewModel

  • vw: (map to MyCompany.MyApp.Modules.Wizard.Views.WizardView )

I think this should be possible using the mapping in ViewLocator or ViewModelLocator or NameTransformer , but I haven't figured it out yet.

I am using the Gemini Framework with Caliburn.Micro v1.5.2 (I plan on upgrading to v2 soon).

Here is one of the things I have tried:

public class NewSpecViewModel : WizardViewModel
{
    // ...
    static NewSpecViewModel()
    {
        // Escape the '.' for the regular expression
        string nsSource = typeof(NewSpecViewModel).FullName.Replace(".", @"\.");
        string nsTarget = typeof(WizardViewModel).FullName;
        nsTarget = nsTarget.Replace("WizardViewModel", "Wizard");
        // nsSource = "MyCompany\\.MyApp\\.Modules\\.NewSpec\\.ViewModels\\.NewSpecViewModel"
        // nsTarget = "MyCompany.MyApp.Modules.Wizard.ViewModels.Wizard"
        ViewLocator.AddTypeMapping(nsSource, null, nsTarget);
    }
    // ...
}

PS I know there are existing Wizard frameworks ( Extended WPF Toolkit , Avalon Wizard , etc), but I don't want to add another 3rd party assembly and the Extended WPF Toolkit Wizard wasn't working properly.

PPS I also want to use this style of base ViewModel/View mapping elsewhere.

Here's [a link] ( https://caliburnmicro.codeplex.com/discussions/398456 ) to right way to do this.

EDIT: Since codeplex is shutting down, here is the code from the discussion:

var defaultLocator = ViewLocator.LocateTypeForModelType;
ViewLocator.LocateTypeForModelType = (modelType, displayLocation, context) =>
{
    var viewType = defaultLocator(modelType, displayLocation, context);
    while (viewType == null && modelType != typeof(object))
    {
        modelType = modelType.BaseType;
        viewType = defaultLocator(modelType, displayLocation, context);
    }
    return viewType;
};

I know it's late... but there is an option to bind the ViewModel to a view directly, and maybe this helps others.

I would also append this binding to the base classes constructor. The following works for me:

public abstract class WizardViewModel {
    protected WizardViewModel() {
        // this --> points the child class
        ViewModelBinder.Bind(this, new WizardView(), null);
    }
}

With this, each child now uses the WizardView (without any additional programming in the child class).

public class NewSpecViewModel : WizardViewModel {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM