Esri Developer Summit March 8 11, 2016 Palm Springs, CA Developing CrossPlatform.NET Apps with ArcGIS Runtime Antti Kajanus Thad Tilton
Session overview Crossplatform options Xamarin Why it s a good option Forms versus Native Incorporating existing.net code Strategies for sharing code Good design to isolate platformspecific code Compiler conditionals Dependency injection Sharing ArcGIS Runtime implementation
Crossplatform The good, the bad, and the ugly Good Makes your app available to more users Enforces good design patterns Bad User experience and quality of your app may vary Requires more testing Ugly Creating platformspecific UIs Handling platform idiosyncrasies (security, bugs, etc)
Options for creating crossplatform apps Html5 and JavaScript: Sencha, PhoneGap, Appcelerator Titanium C# Development: Xamarin, Alpha Anywhere, Unity 3D Crossplatform ArcGIS Runtime SDKs Java,.NET Qt (C++ / QML) Xamarin
Why Xamarin is a good option Fully native ios and Android apps Exposes all functionality of the ios and Android APIs Ability to share the majority of an app s code (60100%) Performance: code is compiled to native binary, not interpreted Immediate updates to support ios and Android releases Support for 3rdparty.NET libraries Visual Studio and C#!
Xamarin s approach Xamarin.iOS / Xamarin.Android (Native) ios + C# Android + C# Windows + C# Shared C# code Shared C# codebase 100% native API access High performance
Xamarin s approach Xamarin.Forms Shared UI code Shared C# code and even more shared code!
It s native ARM Binary NET C# Bindings NET C# Bindings AOT.APP IL + JIT Compile & link.apk It feels native because it is!
Demo: Run a Xamarin app Antti Kajanus
Xamarin options Two primary approaches Xamarin Forms: lots of shared code, less control Use XAML to define the UI Rendered appropriately for each platform Lowest common denominator UI elements Basic crossplatform functionality Xamarin Native: less shared code, more control Customize UIs with platformspecific elements and designers More platformspecific control Native behavior for user interactions
Which one to pick? Xamarin.Forms Xamarin.iOS / Xamarin.Android Apps that require little platformspecific functionality Apps that uses many platformspecific APIs Apps where code sharing is more important than custom UI Apps where custom UX is more important than code sharing Time until delivery Apps that require specialized interaction
Xamarin development environments Windows or Mac Mac OS X Xamarin Studio Windows Visual Studio Xamarin Studio Xamarin Native ios Requires Mac build host Android Xamarin Forms ios Requires Mac build host Android Windows * Mac * * Not available for ArcGIS Runtime for Xamarin apps
Organizing your Xamarin code Individual project for each platform UI and app code ( Views ) One project for shared code (core) Portable Class Library Shared project Note: If using Xamarin Forms, UI (.xaml) can be shared
The ArcGIS Runtime for Xamarin Why codesharing works One Common API surface Same API on Windows Desktop, Windows Universal Platform, ios, and Android Same underlying code, same functionality Most code becomes shareable crossplatform Streamlined Development Changes inherently apply to all platforms All platforms remain in sync Tooling in Visual Studio Shared projects *
Demo: UWP Portal Browser project Antti Kajanus
Good design Is not optional for crossplatform apps!
Organizing your code Platform specific layers User Interface: UI elements and associated controllers Application: Platform specific (nonshared) application logic Common / shareable layers Business: Models and business logic Data: Persisted information in a SQLite database, XML, plain text, etc. Data Access: Logic that wraps the Data to provide CRUD access Service Access: Logic that wraps access to remote services (REST, e.g.)
Xamarin: Sharing code Isolate platformspecific code ios App Application Layer UI Layer Xamarin.iOS SDK assemblies Shared Code Data Layer Databases Data Access Layer Service Access Layer Android App Business Layer Application Layer UI Layer Models Xamarin.Android SDK assemblies Windows Phone App Application Layer UI Layer Services Reference Windows Phone SDK assemblies
Useful design patterns Model View Controller (MVC) and Model View ViewModel (MVVM): Separates application layers according to the UI (view), the data it displays (model), and business / interaction logic (controller, viewmodel). Dependency Injection (Provider): Allows shared code to be written against an abstract class (or interface). Each app passes an instance that provides platformspecific implementation of the abstract class. Singleton: Ensures that only a single instance of an object exists (portal connection, for example). Façade: Provides a simplified view of complex operations by wrapping them with an API. WidgetManager.GetAllWidgets might serve as a façade to a complex operation that retrieves widgets, for example.
Sharing code Adding platform specific logic ios App Application Layer UI Layer IPlatformSpecific implementation Shared Code Data Layer Data Access Layer Service Access Layer Business Layer Android App IPlatformSpecific Application Layer UI Layer Models IPlatformSpecific implementation Windows Phone App Dependency Injection Application Layer UI Layer IPlatformSpecific implementation
Shared projects Share code with several projects in your app solution Benefits Enables sharing of code between multiple projects Compiler directives (#if ANDROID ) for platformspecific code Can use platformspecific assembly references added to app projects Possible disadvantages Shared project produces no output assembly (*.dll) to share PCL
Sharing code Shared Project ios App Application Layer Shared Project Data Access Layer Business Layer Service Access Layer Models Data Layer UI Layer #if IOS // iosspecific code #if ANDROID // Androidspecific code Android App Application Layer UI Layer #if WINDOWS_PHONE // WPspecific code Windows Phone App Compiled into each app Application Layer UI Layer
Demo: Explore a Xamarin Solution Antti Kajanus
Strategies for successful sharing Data Use ArcGIS Runtime geodatabases when possible Use SQLite to store app data Supported natively by ios and Android C# SQLite assembly (open source) for Windows Phone ADO.NET code for data access Rather than using native APIs Reference System.Data.dll and Mono.Data.Sqlite.dll using System.Data; using Mono.Data.Sqlite; //... string dbpath = Path.Combine ( Environment.GetFolderPath (Environment.SpecialFolder.Personal), "items.db3"); bool exists = File.Exists (dbpath); if (!exists) SqliteConnection.CreateFile (dbpath); var connection = new SqliteConnection ("Data Source=" + dbpath); connection.open ();
Strategies for successful sharing File access Beware of platformspecific aspects of file access Restricted access to certain files or locations Ability to access external or shared data Asynchronous only file operations Use classes in System.IO string filepath = System.IO.Path.Combine ( Environment.GetFolderPath (Environment.SpecialFolder.Personal), "MyFile.txt"); System.IO.File.WriteAllText (filepath, "Contents of text file"); Console.WriteLine (System.IO.File.ReadAllText (filepath));
Demo: Crossplatform Portal Browser Antti Kajanus
Strategies for successful sharing Network operations Proactively handle unavailable connectivity Beware of platformspecific aspects of network access Restricted download sizes (20 meg max for ios on 3G, for example) Asynchronous only network operations (Windows Phone) Use System.Net.WebClient, System.Net.Http.HttpClient and System.Net.HttpWebRequest var webclient = new System.Net.WebClient (); webclient.downloadstringcompleted += (sender, e) => { var resultstring = e.result; // do something with downloaded string }; webclient.encoding = System.Text.Encoding.UTF8; webclient.downloadstringasync (new Uri ("http://someserver.com/file.xml"));
Strategies for successful sharing Threading Platformspecific syntax for marshaling to the UI thread Handle with Provider pattern or with compiler conditionals Tasks created with the Parallel Task Library return to their calling thread TaskScheduler.FromCurrentSynchronizationContext gets thread the task was called on using System.Threading.Tasks; void MainThreadMethod () { Task.Factory.StartNew (() => wc.downloadstring ("http://...")).continuewith ( t => label.text = t.result, TaskScheduler.FromCurrentSynchronizationContext() ); }
Demo: Crossplatform Portal Browser (Forms) Antti Kajanus
Summary Xamarin allows you to build native apps for Andriod and ios. Windows platforms that consume the same shared code can be included. ArcGIS Runtime SDK for Xamarin is consistent across supported platforms and hides platforms idiosyncrasies from you. Good design enables the most efficient code sharing. Keep UI and appspecific code in projects that are distinct from the shared code. Separation of concerns, Dependency Injection, Compiler conditionals are specific techniques that help in crossplatform development.
Thank you Questions?