Thursday, 29 October 2020

Lifecycle of an App

The lifecycle of an app contains the sequence of methods that are called since the time the app is launched until the time it is in the memory. The Lifecycle methods allow developers to write code for initialization or logic or transition so that the app components are initialized or show a specific behavior as and when they transit from one state to another. The developer may want to save state, initialize graphical components with data or reset apps state or free memory during these App methods.

The various life cycle methods of XamarinForms Apps are:

  • OnStart: when the application initializes onStart is called. It is the best place to initialize objects
  • OnSleep: Once the application goes to the background, the state is called ‘resume’. The app does not die here rather is kept in memory but with reduced priority. OnSleep() is similar to OnPause() in Android
  • OnResume: OnResume is called when the application is resumed, i.e after being sent to the background. Similar to OnResume on Android.



Custom Renderers

Custom renderers allow the Generic Xamarin.Forms control to be customized in behavior and look as per the platform they are going to be used on. This enables developers to give a native look and behavior to the otherwise Generic Xamarin.Forms Control.  






Xamarin Forms controls are NOT rendered directly on the native platform. Every Xamarin.Forms control has an accompanying renderer for each platform that creates an instance of a native control. The properties from the Xamarin Forms control are translated across to the native control, then the native control is placed in the native layout, which then gets rendered by the platform.










Custom Renderers allow developers to customize the appearance and/or behavior of the control by writing their custom classes. Custom Renderers can be defined to have a custom behavior or appearance for one platform while allowing the default behavior on other platforms or they can be defined for each platform and provide customization for each different platform like iOS, Android, and the Universal Windows Platform (UWP).  If instead of changing the complete behavior and appearance only some trivial changes are required then 'Effects' can be used as an alternative.

















DependencyService

While developing apps with Xamarin.Forms, you will find that certain native platform-specific functionalities are not present in the Xamarin.Forms API. This is because of the generic nature of Xamarin.Forms. Xamarin.Forms allow apps to call into platform-specific functionality from shared code. This functionality enables Xamarin.Forms apps to do anything that a native app can do.   You need to necessarily define an interface and write platform-specific implementations of that interface in the platform project. Dependency service will find the correct implementation of that interface in the various platform projects.   Xamarin.Forms apps need four components to use DependencyService:

  • Interface – The required functionality is defined by an interface in shared code. This needs to be implemented by each platform
  • Implementation Per Platform – The implementation of the above-mentioned interface for each platform.
  • Registration – To find the correct implementation of the interface for the suitable platform at runtime, it is required that each platform implementation  class of the interface be registered with the DependencyService at runtime
  • Invoking Dependency Service- It is required that there be an explicit invocation of the Dependency service, which will then allow the Dependency service to choose the appropriate implementation based on the platform.

   The structure of the application is explained by the following diagram:





Wednesday, 28 October 2020

Creating Scratch View in Xamarin Forms using SkiaSharp

These Days, We could see that coming to a new fashion for gift scratch card which is using many apps like Phone Pay/ Google Pay.


To create a scratch screen in xamarin forms for cross-platform the two basic things we need are the “animation effects” and touch Events in Xamarin.

Forms include its own animation infrastructure that’s straightforward for creating simple animations, while also being versatile enough to create complex animations.

In our requirement we need more than this, so we are a third party library Skiasharp.SkiaSharp for Xamarin.Forms are packaged as a NuGet package. After you’ve created a Xamarin.Forms solution in Visual Studio or Visual Studio for Mac, you can use the NuGet package manager to search for the SkiaSharp.Views.Forms package and add it to your solution. If you check the References section of each project after adding SkiaSharp, you can see that various SkiaSharp libraries have been added to each of the projects in the solution.

In our application, we are using skiasharp for making a rectangle on a canvas and will provide a scratch effect by using SKPath property. Now as we know we have native touch events on each platform for android and iOS but here we using a third-party library “TouchTracking“to provide touch events for cross-platform which will be applicable to android as well as iOS also.

TouchEffect not work in ios with a stacklayout. it is Known issue in TouchEffect.

So for IOS we have to set below Bold Line Code. Sample Code have added this already.


public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
  global::Xamarin.Forms.Forms.Init();
  LoadApplication(new App());

  var _ = new TouchTracking.Forms.iOS.TouchEffect();

  return base.FinishedLaunching(app, options);
}
Download Full Source code :  Link 
Other Sample Link


Monday, 26 October 2020

How To Open An iOS App From A Web Page

 



Biggest Collection for Xamarin Forms Demo Projects

 

Biggest Collection for Xamarin Forms Demo Projects in One link download. 

This consist of Xamarin Forms, Xamarin Android,Xamarin IOS Project . 

All Demo Project also available on GitHub.But this have most of them which is required for any initial developer to learn basic concepts.

Download

Sunday, 25 October 2020

Xamarin Profiler

Xamarin Profiler is a tool which is used by the developers to keep an eye on the information about the particular App inside the Visual Studio. With the help of Xamarin Profiler, developers can easily analyze the App's behavior. We can use the profiler to track the application's memory information and can sample its statistics.

Profiling is an important and often overlooked step in application development. Profiling is a form of dynamic program analysis - it analyzes the program while it is running and in use. A profiler is a data mining tool that collects information about time complexity, the usage of particular methods, and the memory being allocated. A profiler enables you to drill deep and analyze these metrics to pinpoint problem areas in code.

Profiling is about finding resource usage that is out of place. Resources can include, network traffic, CPU, GPU, storage and/or memory usage. Because every app can behave differently, there is no set profile on how resource usage should look for your app, only guidelines.

There are 2 profilers you can use. The Xamarin Profiler for iOS and Android, and the Performance Profiler for UWP apps. The Xamarin Profiler is only available on Enterprise licensing. However the Performance Profiler is available on lower versions. Just another reason, it’s always handy to have a UWP app, for your Xamarin Forms application. More ....

Xamarin Profiler Setup

First you must download the Xamarin Profiler, and install the application.


Once installed, to run the Xamarin Profiler from Visual Studio, all you need to do is go to Analyze > Xamarin Profiler.




But before we do that we need to ensure our iOS and Android apps are setup to be profiled.

Android

Go into the Properties of your Android project, then the Android Options tab, and ensure that Debugging is switched on and the debugger set to Xamarin.

iOS

Go into the Properties of your iOS project, ensure you enable Debugging and Enable Profiling.

Next, go to the Advanced Tab in the same window, and enable the SGen Generational Garbage Collector.

Running Xamarin Profiler

Next, if we run the Profiler, we get to choose which instruments we will use to be profiled, over time. One of the most common metrics you will look at, are the allocations, which also detail the total memory usage.


Once chosen, you will next be presented with the screen that shows the profiling in real time.


At the bottom of the screen, if you selected Allocations, are a list of allocations made. If you are looking at this and not understanding how you are meant to action this data, there is only one thing you really need to look for, and that is anomalies. You look at these details later. But what is an anomaly?

Noticing Anomalies

Anomalies normally come in different trends depending on what is happening. They are something that looks, out of place.

Memory

Memory can have 2 types of anomalies. First is the continuous upward trend. Where every page, or action taken, leads to more memory being consumed, but never released. Second, is large spikes, even if they are being released. This may be due to a large image, particularly in Android, being loaded.

CPU

CPU can show 2 types of patterns. Prolonged 100% CPU usage, or prolonged high CPU usage. This is normally anything above 2-3 seconds would be a concern. 2-3 seconds of 100% CPU usage would be a large concern as this will impact your UI responsiveness.

Second are spikes but you have to look closely. CPU spikes will be common as you move around your app, however if your aren’t interacting with your app and you continuously see spikes still occurring, it may mean a background thread is up to something it shouldn’t be.

App responsiveness and battery life are the two major impacts of CPU usage.

Performance Profiler

The performance profiler comes with Visual Studio and is great to profile UWP apps. I really love having a UWP project, in my Xamarin Forms app, if only for this reason. While it can’t pick out platform specific issues with your native Android and iOS code, it does give a great picture on your non-framework, Xamarin Forms code.

To profile and app, go to Analyze > Performance Profiler

So sadly you can't use the great features of Xamarin Profiler using Visual Studio Community, you need Enterprise. You can also get a trial for 30/90 days if I remember, so consider it.


If you want to profile iOS version of your app, I think you should be able to use Instruments tool from XCode, please follow this instructions: Profiling Xamarin.iOS Applications with Instruments. To open it, go for Tools → Instruments in Visual Studio for Mac.




If you want to profile Android app, check out Profiling Android Apps, it describes how to do it from Android Studio.

Complete Guide: Building a Live Cricket Streaming App for 100M Users

Comprehensive guide to building a scalable live cricket streaming platform for 100M users, covering backend infrastructure, streaming techno...