More Related Content
Similar to Cross-platform Mobile Development (20)
Cross-platform Mobile Development
- 1. Xamarin
Seminar
19th April 2012
Copyright 2012 © Xamarin Inc. All rights reserved
- 2. Agenda
Cross-Platform Mobile Development
Greg Shackles
Senior Software Engineer
OLO Online Ordering
greg@gregshackles.com
gregshackles.com
@gshackles
github.com/gshackles
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 3. Introduction
This session will discuss how to use C# to develop
iOS, Android and Windows Phone applications.
We’ll cover:
•Why use C#?
•Code sharing techniques and patterns
•Useful libraries
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 5. Write Once, Run Anywhere?
!= !=
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 6. C# to the Rescue!
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 7. Benefits
•C# and .NET are mature and powerful
•Skill reuse on all platforms
•Apps are still completely native
•Code can be reused across platforms
(even non-mobile platforms!)
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 8. Architecture
Mono#for#
UI# Silverlight# MonoTouch#
Android#
C## Business#Logic#
Run)me# .NET# Mono#
Pla/orm# WP7# iOS# Android#
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 9. What Code Can Be Shared?
•Most non-UI or platform code
•Core application logic
•Entities
•LINQ (objects, XML)
•Network access (System.Net)
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 10. Requirements
•MonoTouch
•Mac OS X
•MonoDevelop
•Mono for Android
•Mac OS X or Windows
•MonoDevelop or Visual Studio 2010
•Windows Phone
•Windows
•Visual Studio 2010
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 11. File Linking
•All profiles are not created equal
•Single copy of file
•Compile-time verification
•Project Linker extension for VS2010
http://msdn.microsoft.com/en-us/library/ff648745
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 12. Abstraction
•Separate common logic from UI
•Interfaces, base classes, etc.
public interface IContactManager
{
IList<Person> GetContacts();
}
public class AndroidContactManager : IContactManager
{
public IList<Person> GetContacts()
{
return null;
}
} Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 14. Observer Pattern
•Decouple UI from business logic
•Business layer can publish updates
•UI layer subscribes to updates
public EventHandler<EventArgs> MessageReceived;
MessageReceived += (sender, args) =>
{
Console.WriteLine("Message received");
};
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 16. Partial Classes and
Methods
public partial class MyClass
{
// partial methods are private, and must return void
partial void Foo();
public void Bar()
{
Foo();
}
}
public partial class MyClass
{
partial void Foo()
{
Console.WriteLine("Foo");
}
}
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 17. Conditional Compilation
#if __ANDROID__
Console.Write(“Only on Android”);
#elif WINDOWS_PHONE
Console.Write(“Only on Windows Phone”);
#else
Console.Write(“Everything else”);
#endif
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 18. Conditional Compilation
•MonoTouch
•No default symbols
•Mono for Android •Windows Phone
•__ANDROID__ •WINDOWS_PHONE
•__ANDROID_1__ •SILVERLIGHT
•__ANDROID_2__
•...
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 19. File Access
•Direct file access
•System.IO
•Works on iOS and Android
•Not supported by Windows Phone
•Paths are different on iOS and Android
File.WriteAllText(
Environment.GetFolderPath(Environment.SpecialFolder.Personal),
"Writing directly to a file");
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 20. File Access
•Isolated Storage
•Works on iOS, Android, and Windows Phone
•Higher level API
•Don’t need to worry about file paths
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var writer = store.OpenFile("MyFile", FileMode.Create))
{
// write to the file
}
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 21. Database Access
•iOS and Android
•SQLite
•Native APIs or ADO.NET
•Windows Phone
•SQL Server CE
•LINQ to SQL
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 22. Library: C#-SQLite
•C# port of SQLite database
•File based
•Useful for Windows Phone, Silverlight
http://code.google.com/p/csharp-sqlite
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 23. Library: sqlite-net
•Layer over SQLite database
•Strongly-typed queries
•Single file
•Supports iOS, Android, Windows Phone
http://code.google.com/p/sqlite-net
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 24. Library: TinyIoC
•Inversion of Control container
•Single file
•Supports iOS, Android, Windows Phone
https://github.com/grumpydev/TinyIoC
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 25. Library: MonoCross
•Cross-platform MVC framework
•Shared models and controllers
•Views specific to platforms
•Supports iOS, Android, Windows Phone
•Sits atop MonoTouch, Mono for Android
http://code.google.com/p/monocross/
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 26. More Links
MWC 2012 App
https://github.com/xamarin/mobile-samples/tree/master/MWC
NYC Code Camp 6 App
https://github.com/gshackles/NycCodeCamp6
Mobile Development in C#
http://amzn.com/1449320236
Xamarin
Copyright 2012 © Xamarin Inc. All rights reserved
- 27. Xamarin
Seminar
Please give us your feedback
http://bit.ly/xamfeedback
Follow us on Twitter
@XamarinHQ
19th April 2012
Copyright 2012 © Xamarin Inc. All rights reserved
Editor's Notes
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n