Prism 6.1 ViewModelLocator doesn't instantiate my ViewModel

我是研究僧i 提交于 2019-12-02 04:50:47

First, make sure that the views reside in a Views namespace, and the viewmodels in ViewModels, respectively.

Second, the easiest way of debugging this type of things is copying some code from the prism sources and put this in MyBootstrapper.ConfigureContainer:

ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver( x =>
                                                                     {
                                                                         var viewName = x.FullName;
                                                                         viewName = viewName.Replace( ".Views.", ".ViewModels." );
                                                                         var viewAssemblyName = x.GetTypeInfo().Assembly.FullName;
                                                                         var suffix = viewName.EndsWith( "View" ) ? "Model" : "ViewModel";
                                                                         var viewModelName = string.Format( CultureInfo.InvariantCulture, "{0}{1}, {2}", viewName, suffix, viewAssemblyName );
                                                                         return Type.GetType( viewModelName );
                                                                     } );
ViewModelLocationProvider.SetDefaultViewModelFactory( type => Container.Resolve( type ) );

...and put breakpoints there to see exactly what's the problem. Alternatively, dig into your XamlParseException's InnerExceptions... at about the third level you should find the real problem. But I find my breakpoint-approach more convenient.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!