techniques for cross platform .net development
TRANSCRIPT
![Page 1: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/1.jpg)
Techniques for Cross Platform .NET Development
Jeremy Hutchinson@hutchcodes
![Page 2: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/2.jpg)
What is “Cross Platform”
Android PhoneAndroid TabletAndroid Wear
iPhoneiPad
Apple WatchApple TV
Windows PhoneWindows AppsMicrosoft Band
SilverlightXBox
.Net Core.Net (ASP, WPF, Winforms, Etc)
![Page 3: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/3.jpg)
Supported Languages and Tools
Shared Logic
Platform A Platform B
![Page 4: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/4.jpg)
![Page 5: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/5.jpg)
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; } } }}
![Page 6: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/6.jpg)
Linked Files Shared Project* Portable Class
Conditional Compile Partial Classes Inheritance Dependency Injection
Share Code
Manage Platform
Differences
How do we share code?
![Page 7: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/7.jpg)
Linked Files
![Page 8: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/8.jpg)
![Page 9: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/9.jpg)
![Page 10: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/10.jpg)
Shared Projects
![Page 11: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/11.jpg)
![Page 12: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/12.jpg)
![Page 13: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/13.jpg)
![Page 14: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/14.jpg)
![Page 15: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/15.jpg)
UserData.cs
WinPhone8 Project Windows81 Project
Share Project
![Page 16: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/16.jpg)
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; } } }}
![Page 17: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/17.jpg)
Conditional Compilation#if DEBUG public string Foo; #else public string bar; #endif
![Page 18: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/18.jpg)
Conditional Compilation
![Page 19: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/19.jpg)
#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 }
![Page 20: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/20.jpg)
#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 }
![Page 21: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/21.jpg)
#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
![Page 22: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/22.jpg)
Partial Classes
![Page 23: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/23.jpg)
![Page 24: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/24.jpg)
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 } } }
![Page 25: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/25.jpg)
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; } } }}
![Page 26: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/26.jpg)
using System.IO.IsolatedStorage;namespace XPlat{ public partial class UserData { public string UserName { get { return IsolatedStorageSettings.ApplicationSettings["UserName"].ToString(); } set { IsolatedStorageSettings.ApplicationSettings["UserName"] = value; } } }}
![Page 27: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/27.jpg)
![Page 28: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/28.jpg)
![Page 29: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/29.jpg)
![Page 30: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/30.jpg)
Inheritance
![Page 31: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/31.jpg)
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; } } }
![Page 32: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/32.jpg)
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(); } } } }}
![Page 33: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/33.jpg)
What do all these have in common?namespace XPlat
var userData = new XPlat.UserData();var todos = userData.GetUserTodoList();
![Page 34: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/34.jpg)
Dependency Injection
![Page 35: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/35.jpg)
namespace XPlat { public interface IStoredSettings { string UserName { get; set; } } }
![Page 36: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/36.jpg)
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(); } }}
![Page 37: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/37.jpg)
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(); } } }}
![Page 38: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/38.jpg)
var userData = new XPlat.UserData(new XPlat.iOS.AppleSettings());var todos = userData.GetUserTodoList();
![Page 39: Techniques for Cross Platform .NET Development](https://reader036.vdocument.in/reader036/viewer/2022070517/58cecfab1a28abd4098b47a7/html5/thumbnails/39.jpg)
Linked Files Shared Project* Portable Class
Conditional Compile Partial Classes Inheritance Dependency Injection
Share Code
Manage Platform
Differences
How do we share code?