1
votes

Split my view/viewmode/model into separate visual studio projects. I am using MVVMLight toolkit version 5.3.0, visual studio 2015, C# and WPF application for desktop only

solution structure


  • Project 1: UI (WPF application)
  • Project 2: View (WPF user control library - to store all Views)
  • Project 3: ViewModel (Class library - to store all ViewModels)
  • Project 4: Model (Class library - to store all Models)
  • Project 5: Common (Class library - used my Model and ViewModel - stores my - classes and interfaces only)

references


  • Common: referenced by Model and ViewModel projects
  • Model: referenced by ViewModel project
  • ViewModel: referenced by View project
  • View: I assume my UI should reference only my View project

so following MVVM my View knows about ViewModel only an my ViewModel knows about my Model.

so now my questions:

1) In which project do I put the ViewModelLocator? (right now I placed it in my View project) 2) Should my UI project reference only my View project? 3) In the MVVM light project, the "DataContext="{Binding Main, Source={StaticResource Locator}}" is in the View xaml file and the Locator defined in app.xaml. how do I bind my View to my ViewModel using ViewModelLocator?

I've checked several posts and can't figure these answers

Update:

this is what I made to get it work:

step 1: changed the references:

  • UI references View and ViewModel projects
  • View references ViewModel project
  • ViewModel references Model and Common
  • Model references Common

step 2: moved the ViewModelLocator to ViewModel project (from View)

step 3: Added back the datacontext in my View (usercontrol) as DataContext="{Binding UserDetailsVM, Source={StaticResource Locator}}"

step 4: added the to my App.xaml of the UI project

step 5: in the MainWindow of my UI project, I added my view in the xaml as

Notes: - UI project has no references to MVVMLight - Both View and ViewModel projects has reference to MVVLightLibs and CommonServicesLocator

It works, but this is correct way of doing it?

1
I'm pretty sure there is no definitive answer to this question. If the splitting makes sense to you and other developers, then go for it. If you want to see another example how you can do it, I've made a simple example: github.com/famoser/techgemechtel/tree/master/mvvm-pattern/… which I explain here blog.famoser.ch/mvvm-pattern-in-csharpFlorian Moser

1 Answers

0
votes

Honestly, what I'd do is reorganize this into two projects: a Class Library project and a WPF project. So, your current Project 1 and 2 would be in one project, while 3, 4, and 5 are in another.

As to answer your question, I too encountered an issue with ViewModelLocator. I concluded that WPF applications do not like to "share." So, I put the ViewModelLocator in the WPF project.

If you still want all that separation you have going on, I'd suggest using different folders and namespaces under each of the two proposed projects.

Namespace Examples:

  • For your ViewModelLocator, Data Service, Storage Service, Interfaces, etc.: YourAppName.Services
  • For your Models: YourAppName.Models
  • For your ViewModels: YourAppName.ViewModels
  • For your Views: YourAppName.Views