DotNed meeting 22th may 2014: App Development with Xamarin

Yesterdays dotNed meeting was hosted at Macaw and was about App Development with Xamarin. There were two sessions that evening:

  • Session 1: QuickCross - MVVM for Xamarin Apps by Vincent Hoogendoorn
  • Session 2: Best Practices voor Xamarin Android App Development by Sander Egberink

Below are my notes.

Short intro to Xamarin

With Xamarin you can create native iOS, Andriod, Mac and Windows apps in C#. The benefit of it is that you can share code between the platforms so that you can create -for example- a iOS and a Android app based on the same code base. So write once and use on 2.5 billion devices (as the Xamarin marketeers would say). With C# you can use the latest .NET 4.5 features, e.g. sync/await and PCL’s. Coming soon: Universal Application support for VS2013 Update 2. The developer community is very active (about 500k+ developers worldwide), so new native mobile features are also quickly available for iOS and Android.

Development tools

For the IDE’s you have two choices. It’s possible to develop in Visual Studio with a special Xamarin extension. Alternatively you can develop in Xamarin Studio. For each platform it’s advisable / required to have physical appliances. Per platform:

  • Andriod: VS on Windows + Android device
  • iOS: Can use VS on windows for coding & debugging. Requires a MAC + Xamarin Studio for build, deploy and Xcode for UI Visual
  • Windows Phone: Just reuse the shared C# code – no Xamarin libraries or tools are involved.

 

The licensing model for Xamarin is per developer and platform: $999 on a yearly basis. So Visual Studio (Express will not work) + Xamarin + required hardware will add up to a pretty high pricetag to start with. But for organizations this will be a small investment and the ROI will be easy to calculate. Xamarin itself is available as a full functional 30 day trial version.

QuickCross framework

QuickCross is a framework developed by Macaw that works with Xamarin. It is a lightweight (no binaries) cross-platform MVVM (Model View ViewModel) pattern to quickly build native Xamarin.iOS, Xamarin.Android, Windows Phone and Windows Store Apps with shared C# code. The MVVM patterns enable the developer to keep code well structured and flexible for shareable use. QuickCross opens the C# databinding feature towards iOS and Android applications and adds scaffolding. QuickCross is available on GitHub and as a NuGet package.

QuickCross accelerates development with scaffolding by using code snippets. From the Visual Studio package manager console you can use QuickCross commands to generate code: New-ViewModel and New-View. To generate bindable properties, the have include some smart VS code snippets which is a real time server. About 90% of the code can be shared between platforms. Shared components are C# code, PCLs (portable class libraries), NuGet package, Images (using the Splat! library). Seperate per platform are: Views C#, Markup. Platform specific services written in C#. Visual overview of what is cross-platform and what’s platform specific:

QuickCross Pattern

All the QuickCross code is open source and actually used as source in your project. So it’s not a blackbox and you can debug, extend or modify it. Even the scaffolding templates are easily customizable. The team behind QuickCross did a good job about documenting the framework. Getting started, examples, migrating from MvvmCross: it’s all there.

Best practices session

Sander showed us some cases of the Apps that they developed with Xamarin. About online/offline capabilities. How to deal with offline Maps on Android. How to think about multi lingual apps (create bindable properties). How to make you design fluent for longer translated text. Nice to have some insights about the challenges that they encounter. Since it was a long day for me, I didn’t take any notes . At least I took a picture that sums it up :-)

image

Related links

Compare xplatform frameworks and patterns: http://propertycross.com/

QuickCross on GitHub: https://github.com/MacawNL/QuickCross#news

Vincent Hoogendoorn blog: http://vincenth.net/blog/

Using iBeacons with Xamarin: http://vincenth.net/blog/archive/2014/04/24/building-cross-platform-ibeacon-apps-for-ios-android-and-windows-with-c-and-xamarin.aspx

Bing Code Search Add-in for Visual Studio

Bing Code Search for Visual Studio 2013 helps developers find and reuse code samples on the web more easily by bringing the code search experience directly into Visual Studio. With this extension, a developer can search sites such as MSDN, StackOverflow, CSharp411 and Dotnetperls and find, copy and use C# code samples directly inside the IDE.

Download the Code Search Add-in from the Visual Studio gallery site for free.

Make software installation painless with Chocolatey

Problem

Everybody knows the pain of installing a complete new PC system. After installing windows and drivers the issue arrises to install all those useful tools like 7zip, VLC media player, Notepad++ and others. You have to download the installers from websites, wait for completion, unzip, run, install and next, next, next.

Solution

Linux never had this problem, because of apt-get feature. For Windows there now is a solution: Chocolatey! It offers you a seamless and quiet installation quickly from the command line via a central catalog of installation packages. All you need is the main Chocolatey program installed and the package names you want to install. From the command line, you can install Google Chrome, like this:

image

That’s it! It tells Chocolatey to download and install Google Chrome.

The real work behind Chocolatey is done by the NuGet program. The developers have created a set of PowerShell scripts around it. Most important: the installers are downloaded from the official sites. Chocolatey is not changing installers, making installers or hosting the installers. It's automating the boring parts of getting software, but it's still getting that software from the same location as always.

First step: install Chocolatey

To install, open a dos prompt and run:

@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin

After completion the Chocolatey is downloaded and installed in c:\Chocolatey.

Basic list of commands

All Chocolatey commands are run by the command prompt.

Full name Short name & examples Description
choco list clist Display a list of all available packages.
  clist pdf Displays only packages with ‘pdf’ in the title/description.
  clist GoogleChrome –all Displays all the versions of Google Chrome
choco install cinst Install a package silently. Example: ‘cinst GoogleChrome’.
  cinst –notSilent Install a package with the standard wizard. Then more control of where the package is installed & extra options.
choco update all cup all Update all package installed with Chocolatey
choco uninstall cuninst Uninstall a package. Example: ‘cuninst GoogleChrome’
     

Installation feedback

Example of how an installation looks like:

image

Advanced stuff – your own repository

It’s also possible to create/use your own private repository. It can be a local directory, a USB drive, network share with installers in it. Every installer should have a package install script with the extension .nupkg. You can find them in c:\Chocolatey\lib or you can download them from the Chocolatey website:

image 

You can install local packages by using the –source parameter. For a new computer you can use:

image

After installation of all the packages, run this to update all:

image

For the bonus points: Boxstarter offers extra Powershell commands for writing complex installationscripts. Not only for simple Chocolatey packages, but also for installers that require system reboots.

Advanced stuff – Install Windows features

You can also automate installations of Windows features which normally require you to navigate to ‘Add/Remove programs then click Install Windows Features’. To discover the features you can install: ‘clist –source windowsfeatures’.

image

To install the classic ‘Feature’ Telnet Client, just run: cinst –source windowsfeatures TelnetClient

It’s also possible to install WEBpi features (The Web Platform Installer) like this. Again for a full list: ‘clist -source webpi’. To install Umbraco: ‘cinst –source webpi umbraco’

Tip of the day: Random sort a List of objects with Linq

Let’s say you have a list of objects and you want a random presentation to your website visitor. Below is a simple, but effective random algorithm in C# with Linq-to-object by using the GUID type (see line 10 below).

   1:  List<MemorySearchResult> list = Session["MemoryList"] as List<MemorySearchResult>;
   2:              
   3:  // see if list is already in user session
   4:  if (list == null)
   5:  {
   6:      // list not in session, so create a new one
   7:      var newList = myRepository.GetListOfObjects();
   8:   
   9:      // randommize
  10:      list = newList.OrderBy(tempGuid => Guid.NewGuid()).ToList();
  11:   
  12:      // store in session
  13:      Session["MemoryList"] = list;
  14:  }
  15:   
  16:  // now list is randomized and stored in session and ready to use

 

Easy does it!