Upload
jeremy-hutchinson
View
65
Download
0
Embed Size (px)
Citation preview
Techniques for Cross Platform .NET Development
Jeremy Hutchinson@hutchcodes
What is “Cross Platform”
Android PhoneAndroid TabletAndroid Wear
iPhoneiPad
Apple WatchApple TV
Windows PhoneWindows AppsMicrosoft Band
SilverlightXBox
.Net Core.Net (ASP, WPF, Winforms, Etc)
Supported Languages and Tools
Shared Logic
Platform A Platform B
using Windows.Storage;namespace XPlat{ public class UserData { public List<TodoItem> GetUserTodoList() { var userName = this.UserName; //Make a call to a cloud resource } public string UserName { get { return ApplicationData.Current.LocalSettings.Values["UserName"].ToString(); } set { ApplicationData.Current.LocalSettings.Values["UserName"] = value; } } }}
Linked Files Shared Project* Portable Class
Conditional Compile Partial Classes Inheritance Dependency Injection
Share Code
Manage Platform
Differences
How do we share code?
Linked Files
Shared Projects
UserData.cs
WinPhone8 Project Windows81 Project
Share Project
using System.IO.IsolatedStorage;namespace XPlat{ public class UserData { public List<TodoItem> GetUserTodoList() { var userName = this.UserName; //Make a call to a cloud resource } public string UserName { get { return IsolatedStorageSettings.ApplicationSettings["UserName"].ToString(); } set { IsolatedStorageSettings.ApplicationSettings["UserName"] = value; } } }}
Conditional Compilation#if DEBUG public string Foo; #else public string bar; #endif
Conditional Compilation
#if SILVERLIGHT using System.IO.IsolatedStorage; #else using Windows.Storage; #endif namespace XPlat { public class UserData { public string UserName { get { #if SILVERLIGHT return IsolatedStorageSettings.ApplicationSettings["UserName"].ToString(); #else return ApplicationData.Current.LocalSettings.Values["UserName"].ToString(); #endif }
#if SILVERLIGHT using System.IO.IsolatedStorage; #else using Windows.Storage; #endif namespace XPlat { public class UserData { public string UserName { get { #if SILVERLIGHT return IsolatedStorageSettings.ApplicationSettings["UserName"].ToString(); #else return ApplicationData.Current.LocalSettings.Values["UserName"].ToString();#endif }
#if SILVERLIGHT using System.IO.IsolatedStorage; #endif#if NETFX_COREusing Windows.Storage; #endif #if __IOS__using Foundation; #endif #if ANDROIDusing Android.App;using Android.Content; #endif
Partial Classes
namespace XPlat { public partial class UserData { public List<TodoItem> GetUserTodoList() { //Get the username for local storage var userName = this.UserName; //Make a call to a cloud resource } } }
using Windows.Storage;namespace XPlat{ public partial class UserData { public string UserName { get { return ApplicationData.Current.LocalSettings.Values["UserName"].ToString(); } set { ApplicationData.Current.LocalSettings.Values["UserName"] = value; } } }}
using System.IO.IsolatedStorage;namespace XPlat{ public partial class UserData { public string UserName { get { return IsolatedStorageSettings.ApplicationSettings["UserName"].ToString(); } set { IsolatedStorageSettings.ApplicationSettings["UserName"] = value; } } }}
Inheritance
namespace XPlat { public abstract class UserDataBase { public List<TodoItem> GetUserTodoList() { //Get the username for local storage var userName = this.UserName; //Make a call to a cloud resource } public abstract string UserName { get; set; } } }
using Android.App; using Android.Content; namespace XPlat { public class UserData : UserDataBase { private readonly ISharedPreferences _preferences; public UserData() { var ctx = Application.Context; _preferences = ctx.GetSharedPreferences(ctx.PackageName, FileCreationMode.Private); } public override string UserName { get { return _preferences.GetString("UserName", ""); } set { using (var editor = _preferences.Edit()) { editor.PutString("UserName", value); editor.Commit(); } } } }}
What do all these have in common?namespace XPlat
var userData = new XPlat.UserData();var todos = userData.GetUserTodoList();
Dependency Injection
namespace XPlat { public interface IStoredSettings { string UserName { get; set; } } }
namespace XPlat { public class UserData { private readonly IStoredSettings _storedSettings; public UserData(IStoredSettings storedSettings) { _storedSettings = storedSettings; } public List<TodoItem> GetUserTodoList() { //Get the username for local storage var userName = _storedSettings.UserName; //Make a call to a cloud resource throw new NotImplementedException(); } }}
using Foundation; namespace XPlat.iOS { public class AppleSettings : IStoredSettings { public string UserName { get { return NSUserDefaults.StandardUserDefaults.StringForKey("UserName"); } set { var defaults = NSUserDefaults.StandardUserDefaults; defaults.SetString(value, "UserName"); defaults.Synchronize(); } } }}
var userData = new XPlat.UserData(new XPlat.iOS.AppleSettings());var todos = userData.GetUserTodoList();
Linked Files Shared Project* Portable Class
Conditional Compile Partial Classes Inheritance Dependency Injection
Share Code
Manage Platform
Differences
How do we share code?