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

From Objective-C to Xamarin

From Objective-C to Xamarin

This talk is about sharing my experience and impressions about the Xamarin platform. It also covers some common mistakes made by obj-c guys who are switching to C#.

Oleksandr Dodatko

January 24, 2015
Tweet

More Decks by Oleksandr Dodatko

Other Decks in Programming

Transcript

  1. public class RestApiCallFlow { public static async Task<TResult> LoadRequestFromNetworkFlow <TRequest,

    THttpResult, Tresult>( TRequest request, IRestApiCallTasks<TRequest, THttpResult, TResult> stages) { string requestUrl = await stages.BuildRequestUrlForRequestAsync(request); // TODO : check cache THttpResult serverResponse = await stages.SendRequestForUrlAsync(requestUrl); TResult parsedData = await stages.ParseResponseDataAsync(serverResponse); return parsedData; } } Async but Looks Linear
  2. public class RestApiCallFlow { public static async Task<TResult> LoadRequestFromNetworkFlow <TRequest,

    THttpResult, Tresult>( TRequest request, IRestApiCallTasks<TRequest, THttpResult, TResult> stages) { string requestUrl = await stages.BuildRequestUrlForRequestAsync(request); try { TResult parsedDataFromCache = await stages.LoadDataFromCacheForRequestAsync(request); return parsedDataFromCache; } catch { // Load from network code } } }
  3. public interface IRestApiCallTasks<TRequest, THttpResult, TResult> { Task<string> BuildRequestUrlForRequestAsync(TRequest request); Task<THttpResult>

    SendRequestForUrlAsync(string requestUrl); Task<TResult> ParseResponseDataAsync(THttpResult httpData); Task<TResult> LoadDataFromCacheForRequestAsync(TRequest request); }
  4. using (var webclient = new WebClient()) { var imageBytes =

    webclient.DownloadData(url); return UIImage.LoadFromData(NSData.FromArray(imageBytes)); } http://bit.ly/ 1rMJL3B Image Loading : A Typical Implementation
  5. using (var webclient = new WebClient()) { var imageBytes =

    webclient.DownloadData(url); return UIImage.LoadFromData(NSData.FromArray(imageBytes)); } http://bit.ly/ 1rMJL3B Synchronous
  6. using (var webclient = new WebClient()) { var imageBytes =

    webclient.DownloadData(url); return UIImage.LoadFromData(NSData.FromArray(imageBytes)); } http://bit.ly/ 1rMJL3B Leaks
  7. byte[] data = null; using (Stream response = await session.DownloadResourceAsync(request))

    using (MemoryStream responseInMemory = new MemoryStream()) { await response.CopyToAsync(responseInMemory); data = responseInMemory.ToArray(); } BeginInvokeOnMainThread(delegate { using ( NSData imageData = NSData.FromArray(data) ) { using ( UIImage image = new UIImage(imageData) ) { this.ImageView.Image = image; } } }); http://bit.ly/ 1ukw1ii
  8. byte[] data = null; using (Stream response = await session.DownloadResourceAsync(request))

    using (MemoryStream responseInMemory = new MemoryStream()) { await response.CopyToAsync(responseInMemory); data = responseInMemory.ToArray(); } BeginInvokeOnMainThread(delegate { using ( NSData imageData = NSData.FromArray(data) ) { using ( UIImage image = new UIImage(imageData) ) { this.ImageView.Image = image; } } }); http://bit.ly/ 1ukw1ii
  9. byte[] data = null; using (Stream response = await session.DownloadResourceAsync(request))

    using (MemoryStream responseInMemory = new MemoryStream()) { await response.CopyToAsync(responseInMemory); data = responseInMemory.ToArray(); } BeginInvokeOnMainThread(delegate { using ( NSData imageData = NSData.FromArray(data) ) { using ( UIImage image = new UIImage(imageData) ) { this.ImageView.Image = image; } } }); http://bit.ly/ 1ukw1ii
  10. Unlike other cross-platform mobile frameworks that only offer lowest common

    denominator experiences through UI abstraction libraries, we make 100% of the iOS and Android APIs available through our native bindings.