One ViewModel, multiple views

后端 未结 2 951
谎友^
谎友^ 2020-12-30 12:46

I am having a hard time getting multiple views to work against 1 viewmodel. I have read Naming Convention for Multi-View Support without getting much out of it, and have tri

2条回答
  •  庸人自扰
    2020-12-30 13:07

    Anders is correct, there are a number of default conventions for Caliburn.Micro, one of them will locate and display .Views.[]. for .ViewModels.[]..

    In your case, for a single View (assuming the classes reside in namespaces derived from the folders):

    .Views.PeopleView would by located and displayed for .ViewModels.PeopleViewModel.

    For multiple views over the same viewmodel, the convention is that views of format . are displayed for viewmodels of format []ViewModel:

    From your example, you could create a new folder named People, and inside it, create your views named Grid and List.

    Your namespaces become .Views.People.Grid and .Views.People.List and, should then be located and displayed for .ViewModels.PeopleViewModel.

    You typically then display the Views in something like a ContentControl, choosing the View you want to display by setting the cal:View.Context property. You'll either hard code the name, if the context isn't going to change in that particular control, or bind to a property which describes what state the ViewModel should be displayed as.

    e.g.

    
    

    See the Multiple Views over the Same ViewModel section.

提交回复
热议问题