Reputation: 3391
These days I'm working on a new c# project using WPF
and MVVM
related features and tools of .NET platform. I am totally new to this type of application development. After I started learning the stuff like ICommand
and INotifyPropertyChanged
I noticed that as while as I'm digging into these classes and the namespace System.Windows.Input
I'm actually moving away from portability.
Before I stuck with MVVM
I used to create a portable class library for business layer of my application.
Are these MVVM
classes from PresentationCore.dll
well supported in another platforms like Xamarin android and iOS and mac? If I use ICommand
, CommandManager
and other classes in a portable class library am I able to use the implemented library in another mentioned platforms too?
What do I mean by moving away from portability?
For example say we want to develop an application that should be portable to windows and android platforms. when I implement ICommand
I only can use the command with a WPF
control though I easily can call the method in a click event handler of a button in a xamarin android application. So in this example the ICommand is useless in Xamarin android application.
Could you please guide me what should we consider when we are programming MVVM
in a Portable class library? What do MVVM
and PCL
have in common? And When their concepts and features seats uneasy with each other?
Upvotes: 3
Views: 1424
Reputation: 64150
Your portability has nothing to do with WPF.
Commands (ICommand
implementation) and ViewModels (INotifyPropertyChanged
) are portable and don't depend on WPF itself, at least not with the newer .NET Frameworks.
Prism, Microsofts Practice & Patterns MVVM Framework is portable. Works for Desktop, Windows Phone/Mobile, WPF, Silverlight and Xamarin.
Your views aren't meant to be portable. Xamarin released Xamarin Forms a while ago, which is similar to WPF/Windows Phone development as it uses XAML for UI and supports databinding (hence also supports ICommand and ViewModel bindings).
You shouldn't use the old Xamarin UI system if you want to develop an MVVM App.
To ensure, your portable library/assembly containing your viewmodels and commands isn't allow to have any references to Presentation.dll and other WPF assemblies.
If they do, you violate MVVM principle and your code won't be portable. In that case, you got to refactor your code and better understand MVVM. It's definitely possible to have ViewModels and Commands without reference to WPF.
Upvotes: 2
Reputation: 16119
Short answer: portability wasn't one of the primary design goals of WPF. From the MSDN site: "The primary goal of Windows Presentation Foundation (WPF) is to help developers create attractive and effective user interfaces". More specifically, it's to help developers create user interfaces for Windows, hence the W. And given the loose-yet-heavily-implied coupling between the view model and view that means your best bet for portability IMO is in your model and possibly your ORM. So long as you adhere to good SOC practices that's probably the best you're going to get without relying on a 3rd party solution...which more often than not reduces portability further while at the same time adding a whole lot of headache elsewhere.
Upvotes: 3