Top Banner
Cross-Platform Mobile Development that Works. Rob DeRosa Enterprise Customer Success Engineer, Key Accounts
42

Xamarin 9/10 San Diego Meetup

Jan 19, 2017

Download

Technology

Seamgen
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Xamarin 9/10 San Diego Meetup

Cross-Platform Mobile Developmentthat Works.

Rob DeRosaEnterprise Customer Success Engineer,Key Accounts

Page 2: Xamarin 9/10 San Diego Meetup

We’re Here to Help

Rob DeRosa

Enterprise CustomerSuccess Engineer,Key Accounts

[email protected]

Page 3: Xamarin 9/10 San Diego Meetup

DESIGN – DEVELOP - INTEGRATE TEST MONITOR

LEARN

Page 4: Xamarin 9/10 San Diego Meetup

xamarin.com/testcloud

✓ Automated UI testing✓ Thousands of real devices✓ Integration with CI systems✓ Test any mobile app✓ Find bugs before your

customers do

Page 5: Xamarin 9/10 San Diego Meetup

xamarin.com/insights

✓ Crash Reporting✓ Page Views✓ Events✓ User Behavior✓ Integrations

Page 6: Xamarin 9/10 San Diego Meetup

✓ Live, instructor lead courses✓ Developer Certification✓ Guest lectures✓ Office Hours✓ Lightning talks✓ Latest topics

xamarin.com/university

Page 7: Xamarin 9/10 San Diego Meetup

Xamarin Platform

Page 8: Xamarin 9/10 San Diego Meetup

Shared C# codebase • 100% native API access • High performance

iOS C# UI Windows C# UIAndroid C# UI

Shared C# Mobile C# Server

Linux/MonoCoreCLRAzure

Shared C# Client/Server

Page 9: Xamarin 9/10 San Diego Meetup

MapKit UIKit iBeacon CoreGraphics CoreMotion

System.Data System.Windows System.Numerics System.Core System.ServiceModel

System.Net System System.IO System.Linq System.Xml

Page 10: Xamarin 9/10 San Diego Meetup

Text-to-speech ActionBar Printing Framework Renderscript NFC

System.Data System.Windows System.Numerics System.Core System.ServiceModel

System.Net System System.IO System.Linq System.Xml

Page 11: Xamarin 9/10 San Diego Meetup

Microsoft.Phone Microsoft.Networking Windows.Storage Windows.Foundation Microsoft.Devices

System.Data System.Windows System.Numerics System.Core System.ServiceModel

System.Net System System.IO System.Linq System.Xml

Page 12: Xamarin 9/10 San Diego Meetup

Xamarin.iOSXamarin.Android

Page 13: Xamarin 9/10 San Diego Meetup
Page 14: Xamarin 9/10 San Diego Meetup

Traditional Xamarin Approach With Xamarin.Forms:Share UI code, all native

iOS C# UI Windows C# UIAndroid C# UI

Shared C# Business Logic

Shared UI Code

Shared C# Business Logic

Page 15: Xamarin 9/10 San Diego Meetup

✓ 40+ Pages, layouts, and controls(Build with C# or XAML)

✓ Two-way data binding✓ Navigation✓ Animation API✓ Dependency Service✓ Messaging Center✓ Styles, Converters, Behaviors

Shared C# Business Logic

Shared UI Code

Page 16: Xamarin 9/10 San Diego Meetup

<?xml version="1.0"  encoding="UTF-­‐8"?><TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"x:Class="MyApp.MainPage"><TabbedPage.Children><ContentPage Title="Profile"  Icon="Profile.png">

<StackLayout Spacing="20" Padding="20” VerticalOptions="Center"><Entry Placeholder="Username“  Text="{Binding  Username}"/><Entry Placeholder="Password” Text="{Binding  Password}"

IsPassword="true"/><Button Text="Login"  TextColor="White” BackgroundColor="#77D065"

Command="{Binding  LoginCommand}"/></StackLayout>

</ContentPage><ContentPage Title="Settings"  Icon="Settings.png">

<!-­‐-­‐ Settings -­‐-­‐></ContentPage></TabbedPage.Children></TabbedPage>

Page 17: Xamarin 9/10 San Diego Meetup

Layouts

Pages

Stack Absolute Relative Grid ContentView ScrollView Frame

Content MasterDetail Navigation Tabbed Carousel

Page 18: Xamarin 9/10 San Diego Meetup

ActivityIndicator BoxView Button DatePicker Editor

Entry Image Label ListView Map

OpenGLView Picker ProgressBar SearchBar Slider

Stepper TableView TimePicker WebView EntryCell

ImageCell SwitchCell TextCell ViewCell

Page 19: Xamarin 9/10 San Diego Meetup

Xamarin.Forms Ecosystem

Page 20: Xamarin 9/10 San Diego Meetup
Page 21: Xamarin 9/10 San Diego Meetup

Sport

Development goals:

• Provide a way for colleagues to challenge and rank up

• Gain a deeper understanding of Forms – capabilities/limitations

• Learn Azure Mobile Services (WebAPI, EF, push)

Page 22: Xamarin 9/10 San Diego Meetup

Application Design Goals

• Simplified registration/authentication

• Athlete’s can join multiple leagues

• View current rank within ladder

• Athlete stats & challenge history

• Facilitate challenge flow (accept, nudge, post results)

Page 23: Xamarin 9/10 San Diego Meetup

Topics we’ll cover

• Dead-simple authentication with SimpleAuth• Absolutely awesome AbsoluteLayout• Creating & consuming providers (Toast, HUD)• Creating & consuming custom controls• Tricks – ImageButton, styled DateTime picker• Animations and Parallax• Insights & UITest

Page 24: Xamarin 9/10 San Diego Meetup

Front end - Xamarin.Forms

• XAML• Bindings• Converters• Styles

• Reusable Controls• Custom Renderers• Messaging Center• Dependency Service

Back end – Azure Mobile

• WebAPI – REST/JSON• Entity Framework w/

SQL Database• Cross-templated push

notifications• Notification hubs• Tag-based

Page 25: Xamarin 9/10 San Diego Meetup

Demo Time

Page 26: Xamarin 9/10 San Diego Meetup

Absolute Layout

• Used to control size and position of child elements• LayoutBounds is based on LayoutFlags value

• SizeProportional, PositionProportional, All

• Element’s LayoutBounds are proportional to the AbsoluteLayout’s bounds

• The x and y anchor points are interpolated as the child elements position changes

Page 27: Xamarin 9/10 San Diego Meetup

SimpleAuth

• SimpleAuth – stupid simple• github.com/clancey/simpleauth

var scopes = new[] {"https://www.googleapis.com/auth/userinfo.email","https://www.googleapis.com/auth/userinfo.profile"

};var api = new GoogleApi("google", "clientid", "clientsecret") {

Scopes = scopes,};

var account = await api.Authenticate();

Page 28: Xamarin 9/10 San Diego Meetup

Sample

Page 29: Xamarin 9/10 San Diego Meetup

Providers• Use an interface to define intent

• Implement on each platform

• Use the Dependency Service to get a platform-specific

implementation at runtime

• Smaller utilities can use the same namespace/class without

the need for an interface

Page 30: Xamarin 9/10 San Diego Meetup

ToastNotifier Providerpublic interface IToastNotifier{

Task<bool> Notify(ToastNotificationType type, string title, string description,TimeSpan duration, object context = null);void HideAll();

}defin

e

[assembly: Dependency(typeof(Sport.Android.ToastNotifier))]...public class ToastNotifier : IToastNotifier{

public Task<bool> Notify(ToastNotificationType type, string title, string description,TimeSpan duration, object context = null)

{var taskCompletionSource = new TaskCompletionSource<bool>();Toast.MakeText(Forms.Context, description, ToastLength.Short).Show();return taskCompletionSource.Task;

}

public void HideAll(){}

}

impl

emen

t

var toaster = DependencyService.Get<IToastNotifier>();toaster.Notify(type, title ?? type.ToString().ToUpper(), message, TimeSpan.FromSeconds(2.5f));use

Page 31: Xamarin 9/10 San Diego Meetup

Custom, Reusable Controls

• Equivalent to a WPF/ASP.NET User Control

• Use Bindable Properties to allow consumers to bind values

• Templated controls can allow for inner content

• Subclass ContentView

Page 32: Xamarin 9/10 San Diego Meetup

Little Tricks• Use layers for precise layout - position and size

• Use opacity on controls to handle a user event• Examples

• ImageButton

• Styled Date and Time pickers• Use VectorImages instead of bitmaps

• Reduce application footprint by including a single .svg file

• Control tint color and size dynamically• Use an IsBusy flag to trigger progress

• Use a Busy : IDisposable to ensure flag always gets set to false

• Use static converters – less code, fewer objects• Run tasks safely using a proxy task-runner

Let’s check out some code!

Page 33: Xamarin 9/10 San Diego Meetup

Vector Image

• Control size and tint color dynamically• Reduce footprint by resourcing a single .svg file

Page 34: Xamarin 9/10 San Diego Meetup

Animations

• Super easy to use

• Basic animations are supported• Scale• Translate• Fade• Rotate

• Easing (Bounce, Cubic, Linear, Sin)

Page 35: Xamarin 9/10 San Diego Meetup

Sample

Page 36: Xamarin 9/10 San Diego Meetup

Animationsvar speed = (uint)App.AnimationSpeed;var ease = Easing.SinIn;

var pushRect = new Rectangle(Content.Width, btnPush.Bounds.Y, btnPush.Bounds.Width, btnPush.Height);btnPush.FadeTo(0, speed, ease);await btnPush.LayoutTo(pushRect, speed, ease);

var contRect = new Rectangle(Content.Width, btnCont.Bounds.Y, btnCont.Bounds.Width, btnCont.Height);btnCont.FadeTo(0, speed, ease);await btnCont.LayoutTo(contRect, speed, ease);

Page 37: Xamarin 9/10 San Diego Meetup

Parallax Effectvoid Parallax(){

if(_imageHeight <= 0)_imageHeight = photoImage.Height;

var y = scrollView.ScrollY * .4;if(y >= 0){

//Move the Image's Y coordinate a fraction//of the ScrollView's Y positionphotoImage.Scale = 1;photoImage.TranslationY = y;

}else{

//Calculate a scale that equalizes the height vs scrolldouble newHeight = _imageHeight + (scrollView.ScrollY * -1);photoImage.Scale = newHeight / _imageHeight;photoImage.TranslationY = scrollView.ScrollY / 2;

}}

Page 38: Xamarin 9/10 San Diego Meetup

Insights & UITest

• Add custom events or implement at a base level

• Use StyleID to store the element identifier• Share UITest code between iOS and Android

Page 39: Xamarin 9/10 San Diego Meetup

Xamarin Test Recorder

Page 40: Xamarin 9/10 San Diego Meetup

Source Code Analysis

435 lines3.3%

413 lines3.1%

12,471 lines93.6%

Page 41: Xamarin 9/10 San Diego Meetup

Q & A

Page 42: Xamarin 9/10 San Diego Meetup

Thank you!

[email protected]://github.com/xamarin/sport

https://www.youtube.com/watch?v=GmdvxDVluRA