示例#1
0
        public void Resolve_ViewModelType_With_Default_Convention_From_Another_Assembly_With_Custom_Convention()
        {
            var viewModelType = ViewModelLocationProvider.ResolveViewModelType(typeof(MyLibViewB));

            Assert.AreEqual(null, viewModelType);

            ViewModelLocationProvider.ChangeConvention((viewType) =>
            {
                // MvvmLib.Wpf.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
                var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;

                // MvvmLib.Wpf.Tests.View.MyViewB
                var viewName = viewType.FullName;

                // MvvmLib.Wpf.Tests.ViewModel.MyViewB
                viewName = viewName.Replace(".View.", ".ViewModel.");

                // ViewModel
                var suffix = viewName.EndsWith("View") ? "Model" : "ViewModel";

                // MvvmLib.Wpf.Tests.ViewModel.MyViewBViewModel, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
                var viewModelName = string.Format(CultureInfo.InvariantCulture, "{0}{1}, {2}", viewName, suffix, viewAssemblyName);

                var viewModelTypeFound = Type.GetType(viewModelName);
                return(viewModelTypeFound);
            });

            var viewModelType2 = ViewModelLocationProvider.ResolveViewModelType(typeof(MyLibViewB));

            Assert.AreEqual(typeof(MyLibViewBViewModel), viewModelType2);

            ViewModelLocationProvider.ResetConvention();
        }