将派生的ViewModels映射到Caliburn.Micro中的基类View [英] Mapping derived ViewModels to base class View in Caliburn.Micro

查看:85
本文介绍了将派生的ViewModels映射到Caliburn.Micro中的基类View的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的ViewModel和关联的View。我也有多个从基本ViewModel派生的ViewModel,但是我想使用基本View进行显示。

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.

基本ViewModel和视图:

Base ViewModel and View:


  • vm: MyCompany.MyApp.Modules.Wizard.ViewModels.WizardViewModel

  • vw: MyCompany.MyApp.Modules.Wizard.Views.WizardView

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

源自 WizardViewModel


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

  • vw :(映射到 MyCompany.MyApp.Modules。 Wizard.Views.WizardView

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

我认为使用 ViewLocator或ViewModelLocator NameTransformer ,但我还没有弄清楚。

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

我正在使用 Gemini框架 Caliburn.Micro v1.5.2(我计划很快升级到v2)。

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);
    }
    // ...
}

P.S。我知道已有向导向导框架(扩展的WPF工具包 Avalon向导等),但我不想添加其他第三方程序集,而扩展WPF Toolkit向导不是工作正常。

P.S. 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我也想在其他地方使用这种基本的ViewModel / View映射样式。

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

推荐答案

这里是[链接]( https://caliburnmicro.codeplex.com/discussions/398456 )以正确的方式执行此操作。

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

编辑:由于codeplex关闭,因此下面是讨论中的代码:

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;
};

这篇关于将派生的ViewModels映射到Caliburn.Micro中的基类View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆