简体   繁体   English

将派生的ViewModel映射到Caliburn.Micro中的基类View

[英]Mapping derived ViewModels to base class View in Caliburn.Micro

I have a base ViewModel and associated View. 我有一个基本的ViewModel和相关的View。 I also have multiple derived ViewModels from the base ViewModel, but I'd like to use the base View for display. 我还从基础ViewModel中获得了多个派生的ViewModel,但我想使用基本View进行显示。

Base ViewModel and View: 基本ViewModel和View:

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

Derived from WizardViewModel : 源自WizardViewModel

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

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

  • vw: (map to MyCompany.MyApp.Modules.Wizard.Views.WizardView ) vw :(映射到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. 我认为这应该可以使用ViewLocator或ViewModelLocatorNameTransformer中的映射,但我还没有想到它。

I am using the Gemini Framework with Caliburn.Micro v1.5.2 (I plan on upgrading to v2 soon). 我正在使用Gemini FrameworkCaliburn.Micro v1.5.2(我打算很快升级到v2)。

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. PS我知道有现有的向导框架( 扩展的WPF工具包Avalon向导等),但我不想添加另一个第三方程序集,并且扩展的WPF工具包向导无法正常工作。

PPS I also want to use this style of base ViewModel/View mapping elsewhere. PPS我也想在其他地方使用这种基本的ViewModel / View映射方式。

Here's [a link] ( https://caliburnmicro.codeplex.com/discussions/398456 ) to right way to do this. 这是[链接]( https://caliburnmicro.codeplex.com/discussions/398456 )以正确的方式执行此操作。

EDIT: Since codeplex is shutting down, here is the code from the discussion: 编辑:由于codeplex正在关闭,以下是讨论中的代码:

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. 我知道它已经晚了...但是可以选择将ViewModel直接绑定到视图,这可能对其他人有帮助。

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). 有了这个,现在每个孩子都使用WizardView (在子类中没有任何额外的编程)。

public class NewSpecViewModel : WizardViewModel {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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