Slide 1

Slide 1 text

C#6 & MvvmCross Martijn van Dijk @mhvdijk [email protected] github.com/martijn00 speakerdeck.com/martijn00

Slide 2

Slide 2 text

Agenda ■ C# 6 explained ■ What is MvvmCross

Slide 3

Slide 3 text

Do you C# ??? Otherwise you would need some glasses

Slide 4

Slide 4 text

C# 6 ■ C# 6 is the version of C# that ships with VS 2015. ■ The philosophy behind this version is straightforward: improve simple everyday coding scenarios, without adding much conceptual baggage. ■ The features should make code lighter without making the language heavier.

Slide 5

Slide 5 text

Auto-property enhancements ■ Initializers for auto-properties public class Customer { public string First { get; set; } = "Jane"; public string Last { get; set; } = "Doe"; }

Slide 6

Slide 6 text

Auto-property enhancements ■ Getter-only auto-properties public class Customer { public string First { get; } = "Jane"; public string Last { get; } = "Doe"; } public class Customer { public string Name { get; } public Customer(string first, string last) { Name = first + " " + last; } }

Slide 7

Slide 7 text

Expression-bodied function members ■ Expression bodies on method-like members public Point Move(int dx, int dy) => new Point(x + dx, y + dy); public static Complex operator +(Complex a, Complex b) => a.Add(b); public static implicit operator string(Person p) => p.First + " " + p.Last; public void Print() => Console.WriteLine(First + " " + Last);

Slide 8

Slide 8 text

Expression-bodied function members ■ Expression bodies on property-like function members public string Name => First + " " + Last; public Customer this[long id] => store.LookupCustomer(id);

Slide 9

Slide 9 text

Using static using static System.Console; using static System.Math; using static System.DayOfWeek; class Program { static void Main() { WriteLine(Sqrt(3*3 + 4*4)); WriteLine(Friday - Monday); } }

Slide 10

Slide 10 text

Extension methods using static System.Linq.Enumerable; // The type, not the namespace class Program { static void Main() { var range = Range(5, 17); // Ok: not extension var odd = Where(range, i => i % 2 == 1); // Error, not in scope var even = range.Where(i => i % 2 == 0); // Ok } }

Slide 11

Slide 11 text

Null-conditional operators int? length = customers?.Length; // null if customers is null Customer first = customers?[0]; // null if customers is null int length = customers?.Length ?? 0; // 0 if customers is null int? first = customers?[0].Orders.Count(); // new int? first = (customers != null) ? customers[0].Orders.Count() : null; // old int? first = customers?[0].Orders?.Count(); // Chained PropertyChanged?.Invoke(this, args); // Invoke on ?

Slide 12

Slide 12 text

String interpolation var s = String.Format("{0} is {1} year{{s}} old", p.Name, p.Age); var s = $"{p.Name} is {p.Age} year{{s}} old"; var s = $"{p.Name,20} is {p.Age:D3} year{{s}} old"; var s = $"{p.Name} is {p.Age} year{(p.Age == 1 ? "" : "s")} old";

Slide 13

Slide 13 text

nameof expressions if (x == null) throw new ArgumentNullException(nameof(x)); WriteLine(nameof(person.Address.ZipCode)); // prints "ZipCode"

Slide 14

Slide 14 text

Index initializers var numbers = new Dictionary { [7] = "seven", [9] = "nine", [13] = "thirteen" };

Slide 15

Slide 15 text

Exception filters try { … } catch (MyException e) when (myfilter(e)) { … } private static bool Log(Exception e) { /* log it */ ; return false; } … try { … } catch (Exception e) when (Log(e)) {}

Slide 16

Slide 16 text

Await in catch and finally blocks Resource res = null; try { res = await Resource.OpenAsync(…); // You could do this. } catch(ResourceException e) { await Resource.LogAsync(res, e); // Now you can do this … } finally { if (res != null) await res.CloseAsync(); // … and this. }

Slide 17

Slide 17 text

MvvmCross & C#6 Building Native Cross-Platform Apps

Slide 18

Slide 18 text

Why Mvvm? View Binder ViewModel Model Button Text 1. Action 1. Action 3. Command 5. Notify change 6. Change data 4. Access data 2. Event handling

Slide 19

Slide 19 text

Why MvvmCross? ■ Cross-platform awesomeness! ■ Support for all major platforms ■ Most advanced Mvvm library for Xamarin and .NET cross platform ■ Large and engaged community ■ Fast release cycle ■ Clean and easy conventions Miguel de Icaza (Xamarin CTO) “I love MvvmCross” ■ Used by many enterprise companies e.g: - Microsoft - Xamarin inc. - Dutch government - Olo - Nokia Scott Hanselman (Microsoft Developer Evangelist) “I am really impressed with MvvmCross”

Slide 20

Slide 20 text

MvvmCross supported platforms ■ Android ■ iOS ■ Windows ■ Xamarin.Forms ■ tvOS ■ Google wear ■ iWatch ■ Mac

Slide 21

Slide 21 text

MvvmCross details Highlights ■ Flexible architecture ■ PCL based ■ Inversion of Control ■ Dependency injection ■ Value Converters ■ Bindings ■ Testable ■ Plugins Resources ■ Github.com/MvvmCross ■ MvvmCross.com ■ Slack (#mvvmcross) ■ Stackoverflow ■ Xamarin Forums

Slide 22

Slide 22 text

Presenters Specifications ■ Customize view presentation ■ Platform-specific ■ Still retain View Model logic ■ Presentation hints Examples ■ Tabs / Panorama ■ Split View / Master-Detail ■ Fragments ■ Modals ■ Hamburger menu

Slide 23

Slide 23 text

Plugins ■ Messenger ■ Phone Call ■ Picture Chooser ■ SQLite ■ Visibility ■ Web Browser + Many More! MvvmCross ■ Accelerometer ■ Download Cache ■ Email ■ File ■ Json ■ Localization ■ Location Plugins available at https://github.com/MvvmCross/MvvmCross-Plugins

Slide 24

Slide 24 text

Generics ■ MvvmCross uses CoC (Convention over Configuration) by default - LoginView > LoginViewModel ■ Generics can be used too - LoginView : MvxActivity - MainView : MvxActivity ■ Possible to override in setup.cs - protected override IDictionary GetViewModelViewLookup()

Slide 25

Slide 25 text

IoC (Inversion of Control) 1. Use interfaces 2. Define implementation of interfaces at runtime 3. Job done! :) ■ Singleton: Mvx.RegisterSingleton(); ■ Lazy: Mvx.ConstructAndRegisterSingleton(); ■ Dynamic: Mvx.RegisterType(); Mvx.Resolve();

Slide 26

Slide 26 text

Tips ■ Keep it simple ■ Separation of Concerns ■ Don’t try to invent the wheel again, use plugins, samples, etc ■ Use Xamarin player or GenyMotion as emulator for Android ■ Test all changes (UITest, Unit test, etc) ■ Use a common PCL profile (profile7 or profile259) or .NET Platform Standard ■ Enable “Analysis” XS > Preferences > Text Editor > Source Analysis > Enable ■ Get help on Slack: https://xamarinchat.herokuapp.com/ ■ Follow influencers on Twitter #Xamarin #MvvmCross ■ Join the MvvmCross LinkedIn group