The code is available on github... notice that the source data (
restaurants.xml
file) and class file (Objects.cs
) are identical on both platforms, as is the XmlSerialization
code that parses the data into memory for display.You will see there is a lot more 'user code' in the MonoTouch project for iPhone - this is due to the laborious way that
UITableView
s must be coded*; whereas the WP7's Silverlight heritage allows some very neat databinding scenarios.* the excellent
MonoTouch.Dialog
project significantly reduces this code overhead on MonoTouch - definitely give it a try!...now I just need to put the MonoDroid version together... :-)
If I understood correctly, you can reuse the code that you wrote for iPhone with Monotouch for Windows 7 phone? Do you need to buy license for Monodroid even if you have the license for Monotouch?
ReplyDeleteSorry I am a newbie in this :D
You can re-use your C# objects, web service calls, XML processing, Linq queries, etc across all three platforms (WP7, MonoTouch and MonoDroid). Obviously you aren't using Mono on WP7 - just Microsoft's standard c#/.NET environment.
ReplyDeleteFor each platform you write custom UI code that takes advantage of that platform (CocoaTouch for iOS, Silverlight for WP7 and Android's XML/Activity/whatever layout tools).
MonoDroid licensing has not yet been announced as it is still in beta.
Thanks a lot craig for the info. really appreciate it! I will start making iPhone apps with MonoTouch :)
ReplyDeleteCraig, what approach do you recommend for implementing communication between the business layer and the various view engines? I'd like to stick with MVVM, but is that realistic?
ReplyDeleteI think it depends on your definition of MVVM... if you are talking about the Microsoft flavor that relies heavily on the implicit databinding available in XAML (such that you can set the DataContext of a screen to your ViewModel, and things "just work") then that will be hard to replicate on the iOS and Android platforms which lack that capability.
ReplyDeleteHowever, at a more general level, I think you can certainly aim to share your model across all three platforms... but how you bind them to the UI will certainly have to adapt.
The short answer is: I don't think you can write cross-platform ViewModels.
In WP/XAML, you'll probably want to convert collections from the shared code into something Observable (and persist changes back). But otherwise your ViewModel can expose Model properties and expect the databinding to display/set the data - saving you lots of code.
On the other platforms, your binding to the UI will need to do more work to present and persist data on the native UI controls for that platform. Lots of left-to-right assignments will be needed to marshal data from objects to UI and back again.
If your ViewModels are also housing validation or other logic, you can certainly move that up into the shared code - this is what I'd call the Business Logic - then consume those functions in your ViewModel (on WP) or other UI binding.
Hope that makes some sense? What specifically are you concerned about?
There is a more sophisticated example - http://mwc.xamarin.com/ (https://github.com/xamarin/mobile-samples/tree/master/MWC) - that you might find useful. You might like to also read about http://www.MonoCross.net which is a cross-platform *MVC* framework - it's open source so you might get some ideas from that too.