Pages

Friday, 30 October 2020

Xamarin Plugin SharedTransitions

Xamarin.Forms plugin for iOS/Android to enable Shared Transition animations between two pages.

A shared element transition determines how elements that are present in two pages transition between them.

For example, an image that is displayed on both Page A and Page B, transitions from A to B when B becomes visible.

Nuget Link

GitHub Link

Demo Project : Link 

Output : 



Thursday, 29 October 2020

Ways can we share the code - Sharing code overview

 There are three ways we can share code:

  • Shared Project: Here, if required, we write platform specific code using #if compiler directives.
  • Portable Class Libraries:((deprecated - ) Here, we create a PCL targeting the platforms we wish to support and then we use Interfaces & Dependency Services to use platform specific functionality.Use the Shared Asset Project type to organize your source code, and use #if compiler directives as required to manage platform-specific requirements.
  • .NET Standard Libraries: It works similar to the PCL and requires Interfaces to work with platform specific functionality. .NET Standard projects can implement code to be shared across multiple platforms, and can access a large number of .NET APIs (depending on the version). .NET Standard 1.0 - 1.6 implement progressively larger sets of APIs, while .NET Standard 2.0 provides the best coverage of the .NET BCL (including the .NET APIs available in Xamarin apps).

Using Shared Projects

Shared Projects are the default when creating a Windows Store or UWP app so you can write the same code for Windows Store and the Phone. I still think Microsoft was not quite right to set this as default but at least the code will most likely be so similar there would not be much difference between them and hence fewer compiler directives.

With a SharedProject you now create classes as per usual but if you want to do something platform specific you implement a compiler directive around the code. You may implement partial classes but those ifdefs can sneak in.

namespace Chat

    {


         public partial class App : Xamarin.Forms.Application

            {

        #if __IOS__

                public App()

                {

        #else

                public App(Intent screenshareIntent)

                {

        #endif

                    InitializeComponent();

        #if __IOS__

                    MainPage = new NavigationPage(new SessionSelector());

        #else

                    MainPage = new NavigationPage(new SessionSelector(screenshareIntent));

        #endif

                    AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

      }


I can agree that partial classes for Shared Projects can be a nice solution in a small project. However if you have multiple developers or anything more than a really simple app or demo project, maintainability of a Shared Project becomes less than pleasant due to lack of architectural control and struggling to find what is actually running on each platform.

Next a Shared Project suffers from issues when defining an assembly name. For example in xaml if you wanted to reference a control and reference an assembly name, each native project will have a different assembly name, so you either have to do some more ifdefs or OnPlatforms, or have to name each native project the same assembly name.

Benefits

  • Allows you to share code across multiple projects.
  • Shared code can be branched based on the platform using compiler directives (eg. using #if __ANDROID__ , as discussed in the Building Cross Platform Applications document).
  • Application projects can include platform-specific references that the shared code can utilize (such as using Community.CsharpSqlite.WP7 in the Tasky sample for Windows Phone).

Disadvantages

  • Refactorings that affect code inside 'inactive' compiler directives will not update the code inside those directives.
  • Unlike most other project types, a Shared Project has no 'output' assembly. During compilation, the files are treated as part of the referencing project and compiled into that assembly. If you wish to share your code as a assembly then .NET Standard or Portable Class Libraries are a better solution.

Using .NET Standard Class Libraries (SCL)

Note: You may have previously heard of PCL’s, however with .NET Standard, it is now recommended to use .NET Standard Class Libraries, SCL’s.

SCL’s don’t come without some of their own headaches but they provide a great way to write code once, that can be used on multiple platforms, easily maintained and tested. When you write an SCL you don’t need to insert compiler directives to switch code depending on each platform, you select which platforms you will support by choosing which standard version to support.

Which version you choose, depends upon which platform you want to support. For Xamarin apps that need to support Android, iOS and UWP, I choose .NET Standard 1.3. You can find out more information in Microsoft’s .NET Standard documentation.

Benefits

  • Allows you to share code across multiple projects.
  • Refactoring operations always update all affected references.
  • A larger surface area of the .NET Base Class Library (BCL) is available than PCL profiles. In particular, .NET Standard 2.0 has almost the same API surface as the .NET Framework and is recommended for new apps and porting existing PCLs.

Disadvantages

  • Cannot use compiler directives like #if __IOS__.

PCL Vs Shared

  • PCL has an output assembly as DLL while Shared Project has no output assembly.
  • Files inside PCL are treated as part of PCL while in Shared Project, files are treated as part of the referencing project and compiled into that assembly.
  • PCL contains neat and clean platform independent code while Shared Project consists of many #if compiler directives to differentiate code among platforms.
  • PCL is the best approach if good project architecture is the main concern instead of Shared Project.
  • PCL uses the Interfaces and DependencyServices to access platform specific features while Shared Project can assess them directly.
  • PCL has less compile time errors while Shared Project has many chances of compile time errors while switching among platform specific projects.
  • PCL is used if less platform specific code is required. Shared Project is used when lot of platform specific code is required. However, this is not true all the time. Choice is always on the developer.


The most important characteristics of .NET Standard libraries are:

  • They produce a compiled, reusable .dll assembly.
  • They can reference other libraries and have dependencies such as NuGet packages.
  • They can contain XAML files for the user interface definition and C# files.
  • They cannot expose code that leverages specific APIs that are not available on all the platforms targeted by a specific .NET Standard version.
  • They are a better choice when you need to implement architectural patterns such as MVVM, factory, inversion of control (IoC) with dependency injection, and service locator.
  • With regard to Xamarin.Forms, they can use the service locator pattern to implement an abstraction and to invoke platform-specific APIs through platform projects.
  • They are easier to unit test.

PCL it will become obsolete now. .NET Standard is the evolvement of the PCL libraries.

With PCL libraries you could target a number of platforms and only the functionality that was supported for all targeted platforms were available to you. Associated with each combination of platforms were the profiles. A profile was identified by two or three digits. Notable ones for Xamarin were 111 or 259. Read more on PCLs here: https://docs.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/pcl

.NET Standard is a specification of the APIs associated to it. If a platform supports a certain version of the .NET Standard, you are guaranteed that all APIs are available. This way, you can simply target a specific .NET Standard version and each platform that supports it will support your application. The .NET Standard specification has gained a lot of traction and is already more cross-platform than a PCL ever was. Most of the creators of NuGet packages are supporting it already and also Xamarin/Microsoft has replaced the PCL with a .NET Standard library in their templates. Read more on .NET Standard as a concept here: https://docs.microsoft.com/en-us/dotnet/standard/net-standard

So, if you have the possibility it might be wise to start moving to the .NET Standard library. That is, if you want to keep supporting your app and need new libraries coming in. If your app is fine the way it is, you can probably keep going with the PCL for a while. Converting is basically: change the csproj structure to the new structure. Retarget your library to netstandard and reinstall all the libraries you have installed, this time to download the .NET Standard compatible version. The last step might be a bit of a pain.

Coverting PCL to .NetStandard. https://smellyc0de.wordpress.com/2018/03/23/automatically-converting-pcl-to-net-standard-2-0-project/

Flavours of Xamarin

Xamarin allows two different ways of creating applications, based on the amount of code reusability and customization.  

The first approach is the Traditional Native Approach wherein platform-specific apps using Xamarin.iOSiOS and Xamarin.Android can be made. This way of creating apps is generally used when there is a lot of customization specific to the platform is required as it allows direct access to platform-specific APIs. Xamarin.iOS is used for iOS applications and Xamarin.Android is used to create Android applications.

The second approach is creating apps through Xamarin.Forms approach. Xamarin.Forms are used when there is a possibility of reuse of a lot of platform-independent code and the focus is less on custom UI. The platform-independent code is separated and kept in Shared Project or PCL or .NET Standard Library and Platform Specific projects consume this common code by including it.




Pages in Xamarin Forms

Pages are Xamarin Forms generic representation of Cross Mobil Application Screens. A Page occupies most or all of a screen and contains a single child.

On IOS, the Page is mapped to ViewController, on Android, it is mapped to somewhat like Activity and on Universal Windows Platform, it is mapped to  Page. Pages can be of several types, viz. Master /Detail Page, navigational Page, Carousel Page, Tabbed Page, Template Page, etc.



  • Content Page

A Content Page is the most basic type of page which contains the content in a  Stak Layout Grid Layout or Scroll View. The content is usually text or images

  • Master-Detail Page

The Master-Detail Page is divided into two sections: Master and Detail. Master usually contains a list of items or Menu. Clicking on the item would show details about the item on the Detail Page

  • Navigation Page

Navigation Page allows navigating from one page to the next by keeping a stack of all the pages. As the user goes deeper inside the app the pages get stacked one on top of the other and when the user tries to come back to the entry page thee pages are popped out in the reverse order of stacking

  • Tabbed Page

Tabbed Image allows viewing multiple child pages across the same page wherein each Tab represents a child page. One child page is active at a time and it is easier to navigate to the other Tab child page by clicking on the Tab

  • Carousal Page

Just like Tabbed page, Carousel Page is also Multipage. It is similar to the gallery. Only the difference is Instead of clicking on the tab, the Carousal allows swiping gestures on the pages to navigate between the multiple child pages

  • Templated Page

When a user wants to show full mobile screen say for a Game or for showing splash Screen, Template Pages are used. They cover the entire screen area.

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