Caliburn.Micro 自定义View和ViewModel的匹配规则

使用TypeMappingConfiguration类

 1 //Override the default subnamespaces
 2 var config = new TypeMappingConfiguration
 3 {
 4     DefaultSubNamespaceForViewModels = "MyViewModels",
 5     DefaultSubNamespaceForViews = "MyViews"
 6 };
 7  
 8 ViewLocator.ConfigureTypeMappings(config);
 9 ViewModelLocator.ConfigureTypeMappings(config);
10  
11 //Resolves:
12 //MyProject.MyViewModels.CustomerViewModel -> MyProject.MyViews.CustomerView
13 //MyProject.MyViewModels.CustomerPageViewModel -> MyProject.MyViews.CustomerPage
14 //MyProject.MyViews.CustomerView -> MyProject.MyViewModels.CustomerViewModel
15 //MyProject.MyViews.CustomerPage -> MyProject.MyViewModels.CustomerPageViewModel
16 
17 //Change ViewModel naming convention to always exclude View suffix
18 var config = new TypeMappingConfiguration
19 {
20     DefaultSubNamespaceForViewModels = "MyViewModels",
21     DefaultSubNamespaceForViews = "MyViews",
22     IncludeViewSuffixInViewModelNames = false
23 };
24  
25 ViewLocator.ConfigureTypeMappings(config);
26 ViewModelLocator.ConfigureTypeMappings(config);
27  
28 //Resolves:
29 //MyProject.MyViewModels.CustomerViewModel -> MyProject.MyViews.CustomerPage, MyProject.MyViews.CustomerView
30 //MyProject.MyViews.CustomerView -> MyProject.MyViewModels.CustomerViewModel
31 //MyProject.MyViews.CustomerPage -> MyProject.MyViewModels.CustomerViewModel
32 
33 //Change naming conventions to place name suffixes before the base name (i.e. name prefix)
34 var config = new TypeMappingConfiguration
35 {
36     NameFormat = "{1}{0}",
37     IncludeViewSuffixInViewModelNames = false
38 };
39  
40 ViewLocator.ConfigureTypeMappings(config);
41 ViewModelLocator.ConfigureTypeMappings(config);
42  
43 //Resolves:
44 //MyProject.ViewModels.ViewModelCustomer -> MyProject.Views.PageCustomer, MyProject.Views.ViewCustomer
45 //MyProject.Views.ViewCustomer -> MyProject.ViewModels.ViewModelCustomer
46 //MyProject.Views.PageCustomer -> MyProject.ViewModels.ViewModelCustomer
47 
48 //Change naming conventions to omit name suffixes altogether (i.e. distinguish View and ViewModel types by namespace alone)
49 var config = new TypeMappingConfiguration
50 {
51     UseNameSuffixesInMappings = false
52 };
53  
54 ViewLocator.ConfigureTypeMappings(config);
55 ViewModelLocator.ConfigureTypeMappings(config);
56  
57 //Resolves:
58 //MyProject.ViewModels.Customer -> MyProject.Views.Customer
59 //MyProject.Views.Customer -> MyProject.ViewModels.Customer
60 
61 //Configure for Spanish language and semantics
62 var config = new TypeMappingConfiguration()
63 {
64     DefaultSubNamespaceForViewModels = "ModelosDeVistas",
65     DefaultSubNamespaceForViews = "Vistas",
66     ViewModelSuffix = "ModeloDeVista",
67     ViewSuffixList = new List<string>(new string[] { "Vista", "Pagina" }),
68     NameFormat = "{1}{0}",
69     IncludeViewSuffixInViewModelNames = false
70 };
71  
72 ViewLocator.ConfigureTypeMappings(config);
73 ViewModelLocator.ConfigureTypeMappings(config);
74  
75 //Resolves:
76 //MiProyecto.ModelosDeVistas.ModeloDeVistaCliente -> MiProyecto.Vistas.VistaCliente, MiProyecto.Vistas.PaginaCliente
77 //MiProyecto.Vistas.VistaCliente -> MiProyecto.ModelosDeVistas.ModeloDeVistaCliente
78 //MiProyecto.Vistas.PaginaCliente -> MiProyecto.ModelosDeVistas.ModeloDeVistaCliente

 

posted on 2017-11-29 16:34  冰凌灵雨  阅读(545)  评论(0)    收藏  举报

导航