解析ViewModel以收集(不同类型)模型

时间:2012-01-24 14:10:12

标签: c# mvvm prism mef type-conversion

我对与MVISM结合的MVVM模式有点困惑。简而言之:我正在尝试基于由单独服务生成的现有模型创建视图和视图模型。该服务不会也不应该了解有关视图和/或视图模型的任何信息。此服务创建不同类型的模型,为简单起见,我们将其称为SquareModel和CircleModel。这些类型都共享相同的抽象BaseModel。假设该服务生成BaseModel类型的列表,包括Square和Circle模型。现在的问题是我如何将这些模型转换为相应的ViewModel并将它们放在一个新的列表中。每种类型都应该得到它自己的视图模型;这样:

  • SquareModel - > SquareViewModel
  • CircleModel - > CircleViewModel

这是因为两个模型都暴露了我想要使用ViewModel绑定到的不同属性。另外,我如何将这两种ViewModel类型组合到一个列表中以显示给我的视图?

视图包含一个列表框,根据viewmodel类型加载相应的datatemplate。

为了使事情更清楚,我将下面显示的示例代码放在一起,以显示我所做的事情。第一种方法是通过开启类型,第二种方法使用MEF 2.0的ExportFactory。两者都失败了,原因在于代码。我真的很感激任何帮助!

/*
 * Models (these are generated by a service, the service doesn't and should not now about views or view models)
 * 
 * 
 */

abstract class BaseModel { }

class SquareModel : BaseModel { }

class CircleModel : BaseModel { }


/*
 *  View Models
 * 
 * 
 */

abstract class BaseViewModel<TModel> // : INOtificationPropertyChanged, etc
{
    protected TModel Model;

    public void SetModel(TModel model)
    {
        Model = model;
        OnChangeModel();
    }

    protected virtual void OnChangeModel()
    {
        // Assignment of base properties here, based on Model
    }

    // Declarate some base properties here
}

[Export(typeof(BaseViewModel<BaseModel>))]
[TypeMetadata(Type = "CircleViewModel")]
class CircleViewModel : BaseViewModel<CircleModel>
{
    protected override void OnChangeModel()
    {
        // Assignment of circle specific properties here, based on Model
    }

    // Declarate some circle specific properties here
}

[Export(typeof(BaseViewModel<BaseModel>))]
[TypeMetadata(Type = "SquareViewModel")]
class SquareViewModel : BaseViewModel<SquareModel>
{
    protected override void OnChangeModel()
    {
        // Assignment of square specific properties here, based on Model
    }

    // Declarate some square specific properties here
}

class Program
{
    [ImportMany]
    protected IEnumerable<ExportFactory<BaseViewModel<BaseModel>, ITypeMetadata>> Factories { get; set; }

    public BaseViewModel<BaseModel> Create(string viewModelType)
    {
        var factory = (from f in Factories where f.Metadata.Type.Equals(viewModelType) select f).First();

        // Factory is able to create View Models of type viewModelType using CreateExport() function
        var vm = factory.CreateExport().Value;

        return vm;
        // Same error as with solution A
        // cannot convert from 'ConsoleApplication1.SquareViewModel' to 'ConsoleApplication1.BaseViewModel<ConsoleApplication1.BaseModel>'
        // This error is actually displayed in ExportFactory context, but it means the same
    }

    public BaseViewModel<BaseModel> CreateFrom(Type type)
    {
        var vmTypeName = type.Name + "ViewModel";
        return Create(vmTypeName);
    }

    public BaseViewModel<BaseModel> CreateVMUsingExportFactory(BaseModel model)
    {
        var vm = CreateFrom(model.GetType());
        vm.SetModel(model);
        return vm;
    }

    public void DoStuff()
    {
        // Suppose service gives me this
        var serviceOutput = new List<BaseModel>
                                {
                                    new SquareModel(),
                                    new CircleModel(),
                                    new CircleModel(),
                                    new SquareModel(),
                                    new CircleModel(),
                                    new SquareModel(),
                                    new SquareModel()
                                    // may be longer but not the point
                                };

        // viewModelCollection is bound to a listbox, by using datatemplates everthing is nicely placed on the canvas; no problem there
        // Actually this is a ObserveableCollection
        List<BaseViewModel<BaseModel>> viewModelCollection = new List<BaseViewModel<BaseModel>>();


        //
        // What to do here?
        //

        //
        // A. Switch-on-type
        foreach (var model in serviceOutput)
        {
            // Note there are beter implementations of this, using dicationaries and delegates, main goal of that is to not break when refactoring;
            switch (model.GetType().Name)
            {
                case "SquareModel":
                    SquareViewModel vm = new SquareViewModel();
                    vm.SetModel((SquareModel)model); // another cast..... :(
                    viewModelCollection.Add(vm);
                    // Error: 
                    // cannot convert from 'ConsoleApplication1.SquareViewModel' to 'ConsoleApplication1.BaseViewModel<ConsoleApplication1.BaseModel>'

                    break;

                case "CircleModel":
                    // same
                    break;
            }
        }

        // B. MEF ExportFactory<>
        //
        foreach (var model in serviceOutput)
        {
            var vm = CreateVMUsingExportFactory(model);
            viewModelCollection.Add(vm);
        }

        // C. Something else?!
        //
        // Please help ;-).
    }

1 个答案:

答案 0 :(得分:2)

xaml绑定对于对象类型是非常宽容的,你可以使用object而不是定义类型

解决你的datatemplate代码的任何问题(我的头脑不应该是一个问题),使用以下

List<object> viewModelCollection = new List<object>(serviceOutput.Select(model=> CreateVMUsingExportFactory(model) as object));

以上使用linq和lambda来创建对象列表。