C# Can';t在2个部件(UWP、Splat)之间注册视图和视图模型

C# Can';t在2个部件(UWP、Splat)之间注册视图和视图模型,c#,reactiveui,splat,.net-standard-2.0,C#,Reactiveui,Splat,.net Standard 2.0,在引用.Net标准2.0库的UWP项目中,使用最新的RxUI v8预览和Splat 2.0,我无法注册视图和viewmodel,除非它们位于同一程序集中 我有: Locator.CurrentMutable.RegisterLazySingleton(() => new HomeView(), typeof(IViewFor<HomeViewModel>)); 所以它在“核心”组件中寻找HomeView,但它驻留在UWP项目中。这是结构图 我在与您类似的环境中遇到了相同的问题

在引用.Net标准2.0库的UWP项目中,使用最新的RxUI v8预览和Splat 2.0,我无法注册视图和viewmodel,除非它们位于同一程序集中

我有:

Locator.CurrentMutable.RegisterLazySingleton(() => new HomeView(), typeof(IViewFor<HomeViewModel>));
所以它在“核心”组件中寻找HomeView,但它驻留在UWP项目中。这是结构图


我在与您类似的环境中遇到了相同的问题,因为视图类型被重命名并错误地解析为viewmodel的程序集和命名空间。 有关如何确定视图类型名称,请参见第133-134行:

var viewModelTypeName = viewModelType.AssemblyQualifiedName;
var proposedViewTypeName = this.ViewModelToViewFunc(viewModelTypeName);
注意:ViewModelToViewFunc只是一个字符串。替换将“ViewModel”替换为“View”(请参见构造函数)

为了解决这个问题,我的解决方法是创建自己的IViewLocator实现,类似于:

public class MyViewLocator : IViewLocator {
   public MyViewModelLocator(Assembly viewAssembly, string viewNameSpace)
...
   private IViewFor AttemptViewResolutionFor(Type viewModelType, string contract)
    {
        // proposed view type is now based on provided namespace + classname as modified by ViewModelToViewFunc
        if (viewModelType == null) return null;
        var viewModelTypeName = viewModelType.Name;
        var proposedViewTypeName = _viewNamespace + "." +  this.ViewModelToViewFunc(viewModelTypeName);
...

   private IViewFor AttemptViewResolution(string viewTypeName, string contract)
    {
        try
        {
            // resolve view type in the assembly of the view, and not assembly of the viewmodel
            var viewType = _viewAssembly.GetType(viewTypeName); // instead of Reflection.ReallyFindType(viewTypeName, throwOnFailure: false);
...
}
最后,您的自定义viewlocator实现必须向splat注册,以便覆盖DefaultViewLocator实现:

Locator.CurrentMutable.RegisterConstant<IViewLocator>(new MyViewLocator(typeof(SplashView).Namespace, typeof(SplashView).Assembly));
Locator.CurrentMutable.RegisterConstant(新的MyViewLocator(typeof(SplashView).Namespace,typeof(SplashView.Assembly));

Wow!我很惊讶这个bug在最新版本中仍然存在。谢谢你的回答!!顺便说一句,您是否重用了现有的Splat代码并更改了上面的部分,还是完全重写了本节?我重用了现有代码,只更改了上面的部分。
Locator.CurrentMutable.RegisterConstant<IViewLocator>(new MyViewLocator(typeof(SplashView).Namespace, typeof(SplashView).Assembly));