简体   繁体   中英

prism.forms has Xamarin.Forms dependency

In MVVM pattern ViewModel should not have any knowledge about View (and its dependencies).

Prism (MVVM library) for Xamarin.Forms has dependency on Xamarin.Forms so it breaks above rule although prism team claims to fulfill Microsoft patterns & practices. Another libraries does not have such dependencies (for example mvvmcross, freshmvvm)

How to understand it?

EDIT:

  1. According to BraveHeart answer: How hard is to migrate ViewModel project created in Prism for Xamarin.Forms (with Xamarin.Forms dependency) to ViewModel project for WPF?
  2. Will Prism for Xamarin.Forms remove Xamarin.Forms dependency in future?

Interesting point, however not really Accurate.

MVVM as you said is to separate the ViewModel from the View. So what would break the pattern is if we have an instance of the View inside the ViewModel and you start controlling it directly from the viewmodel that would break the MVVM pattern.

Having a library that the view depends on inside the viewmodel will not break the mvvm pattern. Consider the case of having a Json Library in both sides. However, it would limit how cross platform our solution would be. So in your case you will not be able to share your viewmodels with other platforms than Xamarin (eg WPF).

If you really want to make use of Prism and in the same time have the same ViewModels for a WPF, then in Theory this would work:

  1. Move your ViewModels to a separate portable project that has nor Xamarin.Forms reference neither Prism.
  2. Move the Prism.Forms logic to the code behind, things like NavigationService and PageDialogServices can be injected. In the same time, I think, you have to bind the views to the viewmodels by yourself with instantiating the ViewModels and assign them to be your binding contexts. So maybe you would compromise the viewmodel locator.
  3. If you would like to send a message to the user from the ViewModel then just raise an event in the viewmodel that the view listens to it and you can show the message from the view.
  4. This way theoratically you can share the viewmodels with the WPF.

Notes

  • Since WPF and Mobile are very different, then you might have difficulties sharing the Vewmodel, not technical difficulties but rather logical. For example Mobiles are PageBased , and each page would have its own ViewModel in the ideal and the cleaner case . WPF in the other hand is one big view with one big ViewModel. I think the only way to share all the viewmodels from Xamarin and use them effectively in your WPF project is by deviding your WPF project into regions .
  • I am not really 100% sure about the AutoViewModelLocator if it is going to be working or not , it might work depending on the Prism teams by just using the registration. I remember there was one implementation of Prism where you can register the View and it's ViewModel but i do not remember which version. But personally I do not see it as a very big deal to just say BindingContext = _viewmodel; in the codebehind or in Xaml.

ad 1: given the views are somewhat similar, migrating the view models should be easy, perhaps a matter of copy-and-paste-plus-tidying-up

ad 2: I'd be really surprised if that happened, the other way round is more likely, see this comment on github

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM