MVVM light ViewModelLocator是否可以基于视图动态生成视图模型

时间:2017-05-23 23:06:37

标签: c# mvvm mvvm-light viewmodellocator

MVVM light ViewModelLocator是否可以识别正在加载哪个视图然后动态生成视图模型?

public ViewModelLocator()
{
    ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

    //Find the current window/ user control loading
    ContentControl control = 

    //Generate the View model dynamically

    //Register to dynamically created view model
    SimpleIoc.Default.Register<SelectedViewModel>();

}

My Project structure is given below

1 个答案:

答案 0 :(得分:1)

如果要避免ViewModelLocator编码切换到基于命名约定的框架,如MVVMCross。在MVVMCross中,您根本没有定位器,只需创建一个MyViewModel对象和一个MyView视图:框架根据名称约定匹配它们。