Delpin Susai Raj Monday 12 July 2021

Xamarin.Forms - Working with Behaviors in Xamarin.froms

 In this blog post, you will learn how to use Behaviors in Xamarin.Forms App

Introduction

Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.

Behaviors

Behaviors lets you add functionality to user interface controls without having to subclass them. Behaviors are written in code and added to controls in XAML or code.

Behaviors are the best way to validate the user inputs such as email, phone number, etc.

Prerequisites

  • Visual Studio 2017 or later (Windows or Mac)

Let's Start

Setting up a Xamarin.Forms Project

 Start by creating a new Xamarin.Forms project. You will learn more by going through the steps yourself.

Create a new or existing Xamarin forms(.Net standard) Project. With Android and iOS Platform. 

Setup UI

Now, Create a UI for your requirement. I just created a simple entry for validation purposes.

MainPage.xaml

Add Behaviors

Create a new class and inherit from Behavior<T> into the class. Now I'm going to write Behavior to Entry for simple mobile number validation.

Need to implement OnAttachedTo and OnDetachingFrom from Behavior<T>

EntryBehaviour.cs

Consuming Behaviors

Now, Consume your Behavior in XAML UI Element. See below sample.

Ex: xmlns:behaviors="clr-namespace:XamarinApp"

Add Namespace

Add Attach property of Behavior in your XAML Control like below.

Full Code

You can get the full UI code below.

MainPage.Xaml

Result

Conclusion

I hope you have understood how to use Behaviors in Xamarin.Forms App.

Thanks for reading. Please share your comments and feedback. Happy Coding :)

Delpin Susai Raj Thursday 1 July 2021

Xamarin.Forms - App Shortcuts Using Xamarin.Essentials

In this blog post, you will learn how to add shortcuts using Xamarin.Essentials in Xamarin.Forms App

Introduction

Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.

App Actions

Xamarin.Essentials provided one of the best API for App Actions, by clicking the App icon will display the menu option if choose the option app will be navigating the appropriate page or link.

Prerequisites

  • Visual Studio 2017 or later (Windows or Mac)
  • Xamarin.Essentials 1.6 or later

Setting up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project. You will learn more by going through the steps yourself.

Create a new or existing Xamarin forms(.Net standard) Project. With Android and iOS Platform. 

Install Xamarin.Essential Nuget

Note: Make sure you must install Xamarin.Essentials 1.6 or later.

In this step, add Xamarin.Essentials to your project. You can install Xamarin.Essentials via NuGet, or you can browse the source code on GitHub.
Go to Solution Explorer and select your solution. Right-click and select "Manage NuGet Packages for Solution". Search "Xamarin.Essentials" and add Package. Remember to install it for each project (PCL, Android, iO, and UWP). refer below image

Android Setup

Now, Add Intentfilter for App Action in your MainActivity.cs class. Refer to the below image.

MainActivity.cs 


iOS Setup

Add below code in AppDelegate.cs to handle App Action in your iOS app.

AppDelegate.cs

Add Shortcuts

Now add App shortcuts in your App.Xaml.cs class OnStart Method

The following properties you can be set on an AppAction:

  1. Id: A unique identifier used to respond to the action tap.
  2. Title: the visible title to display.
  3. Subtitle: If supported a sub-title to display under the title.
  4. Icon: Must match icons in the corresponding resources directory on each platform.

App.xaml.cs

Responding To Actions

Now, Register actions for shortcuts in App.xaml.cs constructor.

When an app action is selected the event will be sent with information as to which action was selected.

App.xaml.cs


FeatureNotSupportedException

If App Actions are not supported on the specific version of the operating system a FeatureNotSupportedException will be thrown. 

Full code

You will get the full source code here.

App.xaml.cs

Result

iOS Simulator

Android

Conclusion

I hope you have understood how to add shortcuts using Xamarin.Essentials in Xamarin.Forms App

References

https://docs.microsoft.com/en-us/xamarin/essentials/app-actions 

Thanks for reading. Please share your comments and feedback. Happy Coding :)