Upgrade to Pro — share decks privately, control downloads, hide ads and more …

C#6 Explained & MvvmCross!

C#6 Explained & MvvmCross!

Martijn van Dijk

January 15, 2016
Tweet

More Decks by Martijn van Dijk

Other Decks in Technology

Transcript

  1. 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.
  2. Auto-property enhancements ▪ Initializers for auto-properties public class Customer {

    public string First { get; set; } = "Jane"; public string Last { get; set; } = "Doe"; }
  3. 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; } }
  4. 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);
  5. Expression-bodied function members ▪ Expression bodies on property-like function members

    public string Name => First + " " + Last; public Customer this[long id] => store.LookupCustomer(id);
  6. 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); } }
  7. 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 } }
  8. 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 ?
  9. 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";
  10. Index initializers var numbers = new Dictionary<int, string> { [7]

    = "seven", [9] = "nine", [13] = "thirteen" };
  11. 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)) {}
  12. 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. }
  13. 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
  14. 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”
  15. MvvmCross supported platforms ▪ Android ▪ iOS ▪ Windows ▪

    Xamarin.Forms ▪ tvOS ▪ Google wear ▪ iWatch ▪ Mac
  16. 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
  17. 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
  18. 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
  19. Generics ▪ MvvmCross uses CoC (Convention over Configuration) by default

    - LoginView > LoginViewModel ▪ Generics can be used too - LoginView : MvxActivity<LoginViewModel> - MainView : MvxActivity<SomeDifferentNameViewModel> ▪ Possible to override in setup.cs - protected override IDictionary<Type, Type> GetViewModelViewLookup()
  20. IoC (Inversion of Control) 1. Use interfaces 2. Define implementation

    of interfaces at runtime 3. Job done! :) ▪ Singleton: Mvx.RegisterSingleton<T>(); ▪ Lazy: Mvx.ConstructAndRegisterSingleton<T>(); ▪ Dynamic: Mvx.RegisterType<T>(); Mvx.Resolve<T>();
  21. 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