How to organize ViewModel and View projects to use View.Context switching?

StackOverflow https://stackoverflow.com/questions/22410989

  •  14-06-2023
  •  | 
  •  

سؤال

I have my views and viewmodels in separate projects, and I use ViewLocator.AddNamespaceMapping() calls to map everything.

How do I continue to keep things separate and use the View.Context switching recommended here ?

Assume I have my viewmodel project with top level functional folders:

MyViewModels/
---/FormDesigner/FormDesignerViewModel.cs
---/GroupDesigner/GroupDesignerViewModel.cs
---/FilterDesigner/FilterDesignerViewModel.cs
etc.

And my view project like:

MyViews/
---/FormDesigner/FormDesignerView.xaml
---/GroupDesigner/GroupDesignerView.xaml
---/FilterDesigner/FilterDesignerView.xaml

And I currently map in my Bootstrapper like:

ViewLocator.AddNamespaceMapping("MyViewModels", "MyViews);

How would I introduce View.Context switching for alternate views for each of my current Xaml views? Thanks

هل كانت مفيدة؟

المحلول

I believe the built in conventions will take care of it for you (you could also specify additional conventions), provided your views are namespaced/named correctly (See "Multiple Views over the Same ViewModel").

Usually, you would place your views (if you wanted switching) in a structure like:

Folder:    Views/FormDesigner/         // Folder Structure not critical
NameSpace: YourApp.Views.FormDesigner
XAML:      YourApp.Views.FormDesigner.Master.Xaml
XAML:      YourApp.Views.FormDesigner.Detail.Xaml
Binding:   <ContentControl cal:View.Model="{Binding Path=ActiveItem}" 
               cal:View.Context="Master" />

Your binding may vary, and for switching, you'll want to bind the Context to a property on the ViewModel (which indicates either Master, or Detail in your situation.

You can use your existing view, but you'll have to change the name from FormDesignerView.xaml if you want to switch the context.

I had a crack at explaining the built in Caliburn.Micro conventions governing that behaviour in this answer: One ViewModel, multiple views

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top