![Page 1: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/1.jpg)
C# for Android and iOS
Xamarin
![Page 2: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/2.jpg)
InhaltSie sind erfahrener C#-Entwickler, die Welt von Android und iOS reizt
Sie aber trotzdem? In dieser Session zeigt Ihnen Rainer Stropek, wie Sie
mit den Xamarin-Tools Ihr C#-Wissen auf diese mobilen Plattformen
mitnehmen können. Rainer stellt Ihnen die Tools vor und demonstriert an
einem durchgängigen Beispiel, wie plattformübergreifende C#-
Codewiederverwendung funktionieren kann.
![Page 3: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/3.jpg)
XamarinIntroduction
Agenda
DevelopXamarin Studio
Visual Studio
Debugging
ExampleApp dev basics
Native APIs
Azure component
Code sharing
SummaryKey takeaways
Bildquelle:
http://www.xamarin.com
Bildquelle:
Screenshot Xamarin Studio
Bildquelle:
http://www.flickr.com/photos/caveman
_92223/3347745000/
![Page 4: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/4.jpg)
IntroductionWhat‘s Xamarin and what problems does it solve?
![Page 5: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/5.jpg)
What Problem Does Xamarin Address?
Need to support a broad range of mobile devicesDifferent platforms – Android, iOS, Windows Phone
Different devices – smartphones, tablets
Existing C# knowledge and experienceSkilled C#/.NET developers and existing C#/.NET codebase
Lack of knowledge about native development Java, Objective-C
Can we cover mobile device market with our existing knowledge and
tools?
![Page 6: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/6.jpg)
Potential Solutions Build native apps
Requires knowledge about C#, Java, and/or Objective-C
Requires knowledge about the target platform
Build mobile web sitesDoes it feel like a real native app?
Lack of possibilities to fully use the underlying platform?
Use a cross-platform development tookitE.g. Phonegap
Xamarin: Existing tools & knowledge with bridge to native APIs
![Page 7: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/7.jpg)
What is Xamarin? Company founded by the initiators of the Mono project
C# + Runtime + .NET BCLC# Compiler
Implementation of the Common Language Infrastructure for Linux-based systems
.NET Base Class Library
Bridges to native APICallable Wrappers
Development environmentsXamarin Studio
Visual Studio integration
Component store
![Page 8: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/8.jpg)
• Xamarin is only free for very small apps
• Pricing per year and per developer
• Prices on the right as per Sept. 24th 2013
• For up-to-date prices see Xamarin Store
Pricing
![Page 9: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/9.jpg)
Bridgespublic class ContactListCursorAdapter extends BaseAdapter {
private Context mContext;private List<ContactEntry> mItems = new ArrayList<ContactEntry>();
public ContactListCursorAdapter(Context context, ArrayList<ContactEntry> items) {
mContext = context;mItems = items;
}
public int getCount() {return mItems .size();
}
public Object getItem(int position) {return mItems .get(position);
}
public long getItemId(int position) {return position;
}
public View getView(int position, View convertView, ViewGroup parent) {…}}
Java Example
![Page 10: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/10.jpg)
Bridgespublic class HiveAdapter : BaseAdapter<Hive>{
private IReadOnlyList<Hive> items = new List<Hive>();
public HiveAdapter(Context context) {…
}
public override int Count { get { return this.items.Count; } }
public override Hive this[int position] { get { return this.items[position]; } }
public override long GetItemId(int position) { return this.items[position].Id; }
public override View GetView(int position, View convertView, ViewGroup parent)
{…
}}
C# Example
getCount becomes Count
property
getItem becomes indexer
getItemId becomes GetItemId
getView becomes GetView
![Page 11: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/11.jpg)
Bridges// JAVA =======================================================someTextView.setOnClickListener(new View.OnClickListener() {
public void onClick() {//Do Stuff
}});
// C# =========================================================itemTextView.Click += (o, e) => {
// Do Stuff};
Events
Java Listener Interfaces
become C# events
![Page 12: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/12.jpg)
Wrapper LifetimeIDisposable
Note that all classes derived
from Java.Lang.Object
implement IDisposable
![Page 13: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/13.jpg)
Linker
Removes unused APIs to
reduce size of appSee Xamarin docs for details
Shared runtime during
debuggingReduces package size
Speedup app deployment and
startup during debugging
![Page 14: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/14.jpg)
DevelopmentIDEs, Debugging
![Page 15: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/15.jpg)
Xamarin Studio
Works on Windows and Mac
Basics similar to Visual StudioIf you know VS, you will immediately
understand it
By far not that powerful as VS
Identical project/solution file
formatOpen project/solution files in both IDEs
as you need it
![Page 16: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/16.jpg)
Visual Studio
Works only on Windows
The full IDE experience you are
used toSame C# editor
Same UI
Full deployment and
debugging support
![Page 17: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/17.jpg)
ComponentsXamarin Components
Full list of components see
Xamarin Components
website
![Page 18: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/18.jpg)
EmulatorDebug and test your app
Android Device EmulatorNot specific to Xamarin
All features, tools, and restrictions of
native Android development apply
ARM or Intel-based imagesARM images are very slow
Recommendation: Intel image with
HAXM
![Page 19: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/19.jpg)
Demo
Hello World!
Sample app in Xamarin Studio and Visual Studio
Comparing the developer
environments
Debugging experience
![Page 20: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/20.jpg)
Sample
![Page 21: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/21.jpg)
Create ProjectCreate and configure project
![Page 22: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/22.jpg)
Create ProjectSetup project dependencies
![Page 23: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/23.jpg)
Data Access LayerPlatform-independent code
Abstract base classCould be in a separate class library
(PCL)
Use DBConnection to keep code
reusable
Other strategiesLink source files
Use partial classes
Use interfaces to isolate platform-
specific aspects in your code
Conditional compile
Use patterns like MVVM to reduce
amount of platform-specific code
![Page 24: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/24.jpg)
Data Access LayerPlatform-independent code
Implementation for mobile
deviceSQLite
NoteFull support for MEF
Async APIs
C# 5 async/await
![Page 25: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/25.jpg)
Data Access LayerPlatform-independent code
Implementation for
SQL ServerWindows Azure SQL Database
![Page 26: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/26.jpg)
Add Data AccessAdd existing items
Mobile DAL
implementation
![Page 27: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/27.jpg)
Add UIUI for each list item
![Page 28: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/28.jpg)
Add AdapterData Source for ListView
NoteUse of async/await
Use of Intent
![Page 29: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/29.jpg)
Main ActivityEntry point for your app
NoteRenamed Activity1 to MainActivity
Change base class to ListActivity
![Page 30: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/30.jpg)
UI for DetailsAdd second Activity
![Page 31: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/31.jpg)
UI for DetailsAdd second Activity
NoteIntent to launch external program
(browser)
![Page 32: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/32.jpg)
UI for DetailsAdd second Activity
NoteIntent to launch external program
(browser)
![Page 33: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/33.jpg)
Action BarAdd menu for action bar
![Page 34: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/34.jpg)
Azure Mobile ServiceBackend for app in the cloud
Create Azure Mobile
ServiceIn the background your data is
stored in SQL Server
![Page 35: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/35.jpg)
Azure Mobile ServiceBackend for app in the cloud
Add table HiveIn the background you are
creating a table in SQL Server
Mobile Services does not need a
schema no need to create
columns in the table
![Page 36: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/36.jpg)
Azure Mobile ServiceBackend for app in the cloud
Get URL and application
key
![Page 37: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/37.jpg)
Add Sync Code
Sync triggered by action
bar button
![Page 38: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/38.jpg)
Check Result
Run the app and check
result of sync in Windows
Azure SQL Database
![Page 39: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/39.jpg)
Summary
![Page 40: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/40.jpg)
Summary Great to bring existing C# knowledge to mobile platforms
Existing business logic C# code might be reused
Write once run anywhere is true for most business logic code
You still have to learn and understand the platformActivities, Intents, Services, Adapters, Android SDK, etc.
No or little code sharing for UI markup/codeCan be maximized using MVVM approach
![Page 41: C# for Android and iOS - software-architects.com · What is Xamarin? Company founded by the initiators of the Mono project C# + Runtime + .NET BCL C# Compiler Implementation of the](https://reader034.vdocument.in/reader034/viewer/2022042220/5ec6e667dfb0962295377956/html5/thumbnails/41.jpg)
Saves the day.
BASTA 2013 – C# Workshop
F&A
Rainer Stropeksoftware architects gmbh
http://www.timecockpit.com
@rstropek
Danke für euer Kommen
Web