You want to write mobile apps, but you don’t want to write the same code four times over. You want cross-platform apps, but you don’t want to sacrifice the native speed and power. You want to create native iOS, Android, Windows Phone, and Windows 8 apps, and you want it all while maximizing code reuse and harnessing the power and simplicity of C#. You want four native platforms, but you want one codebase.
Of course you do. You want Xamarin.
14. NSString *string = @“ spaces in front and at the end “;
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
<Intro>
I think C# is a beautiful language, and I’m amazed at how it has grown and evolved over the years, taking us places we never thought about before.
Tonight, I’m going demonstrate what I find to be the most exciting place to find C#; on our mobile devices. ALL of our mobile devices. I assume everyone here has a mobile device. How many have an iOS device? (iPhone/iPad)? Android phone or tablet? Windows Phone or Windows RT tablet? Windows 8? Mac Desktop? All of these platforms and devices are candidates to be running C# thanks to Xamarin.
Ask: How much does the typical mobile app cost to create? $1000? $2000? $5000? (Wait for answers)
According to a recent study, 80% of apps cost over $25,000 to create. Over HALF of all apps cost more than $50,000 to create. And that is per app. But, as we all know, it's a fragmented market out there.
Android accounts for 52% of devices, iOS brings in another 40%, and "Other", including Windows Phone makes up the other 8%. But even these numbers are fragmented. There are five major versions of Android in the wild at the moment. iOS has three major versions out now. There are six different iOS screen resolutions, and an infinite number of Android resolutions. Windows Phone is coming out with newer, bigger phones soon, and Windows 8 apps can be any size at all. When each app costs $25,000-$100,000 to create, the cost of designing and developing for each platform individually can skyrocket out of control. You’re left with a difficult decision…target one or two major platforms and leave a significant percentage of potential customers out in the cold, incur the cost to create an app for every platform, or find another way.
Source : AnyPresence Mobile Readiness Report 2013
Let’s talk about the state of mobile development today
Expensive & Slow
Positive = Great apps delivered to user’s platform
Negative = Development hampered by multiple code bases & fragmentation
Unhappy users
Unhappy developers
Increase in abandoned apps
Limited to what is implemented
four years old
The #1 benefit is that we get to share our code between platforms. And not just a little bit. There are multiple case studies showing it’s easy to get 70%-90% code reuse between platforms. This dramatically cuts down on costs as well as bugs.
Second, as .net developers, we get to use our existing .net skills. As a business that already has .net developers and is getting started building our first mobile apps, we can either pay for an Objective-C developer, a Java developer, and our existing .net developers, or we can have that same .net dev, with their accumulated business domain knowledge working across all platforms.
Third, since Xamarin is based on mono and C#, we get a modern programming language. This means that we get async/await, linq, generics, lambdas, nuget, and packages such as json.net, restsharp, mvvmcross, and more.
Fourth, we get powerful IDE support. With Xamarin we can choose between Xamarin Studio, a rich IDE which runs natively on Mac and Windows, or we can use Visual Studio, one of the most popular and powerful IDEs on the planet. This allows us to have a single, familiar IDE to develop in and share code between our iOS, Android, Windows Phone, Windows Store apps.
Lastly, with Xamarin, we get extensive support and documentation.
An important distinction between Xamarin and other frameworks is that Xamarin is completely, 100% native. The C# code that we write ultimately compiles down to native binaries on the target platforms. Unlike other systems such as PhoneGap or Titanium, with Xamarin we get full access to the underlying OS. Anything that Objective-C or Java can do, Xamarin can do. You are not limited in any way. Xamarin’s approach also brings with it the native UI toolkits for each platform. Xamarin does not abstract the user interface away, leaving you with the lowest common denominator for user interfaces. Each UI will still be custom developed and perfected for each platform, ensuring that your apps are beautiful, fast, and native.
Let’s talk a bit about C# and why it is awesome.
Objective-C to get the date from a date time variable
C# to get the date from a date time variable
Objective-C to trim spaces from a string
C# to trim spaces from a string
Objective-C to find a contact in address book
Java to find a contact in address book
C# to find a contact in address book
Let's take a step back, and talk about Mvvm
Mvvm is a GUI design pattern which originated at Microsoft in 2005, when they were first designing WPF and, later, Silverlight. According to John Gossman, one of the main designers of Mvvm, "Model/View/ViewModel is a variation of Model/View/Controller (MVC) that is tailored for modern UI development platforms where the View is the responsibility of a designer rather than a classic developer." Mvvm is all about binding. We can easily bind properties, collections, and actions (or commands). This declarative binding allows us to easily separate the presentation (the view) from the behavior.
In Mvvm, the view is only concerned with the presentation of elements on the screen. The model, as in MVC, could also be called the data model. It represents the intrinsic shape of the data. The ViewModel sits in between the view and the model. It handles loading content the UI needs,hiding and showing content, Date/number/string formatting, responding to the user, performing commands, saving and retrieving data.
This separation of the View from the behavior, via data binding is perfect for our cross platform world. This allows us to reuse the logic in our view models, while continuing to have completely native UIs for iPhone, iPad, Android phones, Android tables, WPF, WinRT, Silverlight, Mac desktop, and Windows Phone devices. This also allows us to have designers work on making the iOS and Android UIs pretty, while allowing developers to create the Windows Phone and Windows 8 UIs.
So, given the theory behind Mvvm, how does MvvmCross help us?
MvvmCross is an open source cross platform Mvvm framework written by Stuart Lodge. It's currently on its third version, and has a very active and healthy community around it.
The beauty of MvvmCross is that it supports iOS, Android, Windows Phone 7, Windows Phone 8, Mac Desktop, WPF, Silverlight, Windows 8/RT, and Powershell. This allows us to write the core logic of our app, and reuse it completely across eleven platforms. I'll be demonstrating iPhone, Windows Phone, Windows 8, and Android today. MvvmCross focuses on shared behavior, not presentation. Mvvm itself was introduced with WPF, but then left to stagnate by Microsoft. There haven't been any meaningful updates to the XAML syntax since WPF was first released.
MvvmCross adds powerful binding options to XAML, as well as introducing binding to iOS and Android platforms. Here we see an example of binding a property in our ViewModel it each View.
MvvmCross includes a pluggable architecture, and comes with an IOC container which runs on all platforms. Due the extensibility and pluggable architecture, there are plugins on nuget for json, working with local files, sqlite, message bus, location (gps), web browser, network utilities, email, and many more.