Showing posts with label Xamarin Blog. Show all posts
Showing posts with label Xamarin Blog. Show all posts
Delpin Susai Raj Thursday, 25 November 2021

Xamarin.Forms - Validation using Xamarin Community Toolkit Part 2

In this blog post, you will learn how to validate xaml elements using Xamarin Community Toolkit in Xamarin.Forms App

Introduction

Part 1: https://xamarinmonkeys.blogspot.com/2021/11/xamarinforms-validation-using-xamarin.html

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.

Xamarin Community Toolkit

Xamarin Community Toolkit is a collection of reusable elements for mobile development with Xamarin.Forms, including animations, behaviors, converters, effects, and helpers. It simplifies and demonstrates common developer tasks when building iOS, Android, macOS, WPF and UWP apps using Xamarin.Forms.

The Xamarin Community Toolkit is available as a Visual Studio NuGet package for new or existing Xamarin.Forms projects.

Validation Behaviours

1. CharactersValidationBehavior

2. EmailValidationBehavior

3. MultiValidationBehavior

4. NumericValidationBehavior

5. RequiredStringValidationBehavior

6. TextValidationBehavior

7. UriValidationBehavior

References

  • https://docs.microsoft.com/en-us/xamarin/community-toolkit/behaviors/
  • https://github.com/xamarin/XamarinCommunityToolkit

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. 

Install Xamarin.CommunityToolkit NuGet

Install the following Nuget from Nuget Manager In your Visual Studio.

Xamarin.CommunityToolkit

NumericValidation

NumericValidationBehavior is a behavior that allows the user to determine if text input is a valid numeric value. For example, an Entry control can be styled differently depending on whether a valid or an invalid numeric input is provided. 

Xaml code

More details

https://docs.microsoft.com/en-us/xamarin/community-toolkit/behaviors/numericvalidationbehavior

Result

RequiredStringValidation

RequiredStringValidationBehavior is a behavior that allows the user to determine if text input is equal to specific text. For example, an Entry control can be styled differently depending on whether a valid or an invalid text input is provided.

Xaml code

More details

https://docs.microsoft.com/en-us/xamarin/community-toolkit/behaviors/requiredstringvalidationbehavior

Result

TextValidation

TextValidationBehavior is a behavior that allows the user to validate a given text depending on specified parameters. By adding this behavior to an Entry control it can be styled differently depending on whether a valid or an invalid text value is provided.

Xaml Code

More details

https://docs.microsoft.com/en-us/xamarin/community-toolkit/behaviors/textvalidationbehavior

Result


UriValidation

UriValidationBehavior is a behavior that allows users to determine whether or not text input is a valid URI. For example, an Entry control can be styled differently depending on whether a valid or an invalid URI is provided.

Xaml code

More details

https://docs.microsoft.com/en-us/xamarin/community-toolkit/behaviors/urivalidationbehavior

Result

Conclusion

I hope you have understood how to validate xaml elements using Xamarin Community Toolkit in Xamarin.Forms App

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

Delpin Susai Raj Thursday, 11 November 2021

Xamarin.Forms - Validation using Xamarin Community Toolkit

In this blog post, you will learn how to validate xaml elements using Xamarin Community Toolkit 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.

Xamarin Community Toolkit

Xamarin Community Toolkit is a collection of reusable elements for mobile development with Xamarin.Forms, including animations, behaviors, converters, effects, and helpers. It simplifies and demonstrates common developer tasks when building iOS, Android, macOS, WPF and UWP apps using Xamarin.Forms.

The Xamarin Community Toolkit is available as a Visual Studio NuGet package for new or existing Xamarin.Forms projects.

Validation Behaviours

  1. CharactersValidationBehavior
  2. EmailValidationBehavior
  3. MultiValidationBehavior
  4. NumericValidationBehavior
  5. RequiredStringValidationBehavior
  6. TextValidationBehavior
  7. UriValidationBehavior

References

  • https://docs.microsoft.com/en-us/xamarin/community-toolkit/behaviors/
  • https://github.com/xamarin/XamarinCommunityToolkit

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. 

Install Xamarin.CommunityToolkit NuGet

Install the following Nuget from Nuget Manager In your Visual Studio.

  • Xamarin.CommunityToolkit

CharactersValidation

CharactersValidationBehavior is a behavior that allows the user to validate text input depending on specified parameters. 

Following CharacterType you can validate

Xaml Code

Here the sample is validating Numeric with 5 character.

Result

The value should below 5 char only if it's greater than 5 it's shows error when you submit.

More info

https://docs.microsoft.com/en-us/xamarin/community-toolkit/behaviors/charactersvalidationbehavior

EmailValidation

EmailValidationBehavior is a behavior that allows users to determine whether or not text input is a valid email address.

Xaml code

Regex validation

More info

https://docs.microsoft.com/en-us/xamarin/community-toolkit/behaviors/emailvalidationbehavior

Result

MultiValidation

MultiValidationBehavior is a behavior that allows the user to combine multiple validators to validate text input depending on specified parameters. For example, an Entry control can be styled differently depending on whether a valid or an invalid text input is provided.

Xaml code

More info

https://docs.microsoft.com/en-us/xamarin/community-toolkit/behaviors/multivalidationbehavior

Result

Conclusion

I hope you have understood how to validate xaml elements using Xamarin Community Toolkit in Xamarin.Forms App

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


Delpin Susai Raj Wednesday, 27 October 2021

Xamarin.Forms - Validation using Fluent Validation in MVVM

In this blog post, you will learn how to use fluent validation in MVVM 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.

Fluent Validation

Fluent Validation is one of the best Libraries for Xamarin.Forms it's used to validate XAML Controls. Fluent Validation is used to create the validation logic separate from business logic. Fluent validation is used to lambda expressions for building validation rules for your business objects.

References

https://github.com/james-russo/XamarinForms-UnobtrusiveValidationPlugin

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 login page for validation purposes.

LoginPage.xaml

LoginPageViewModel

Here, you can write your business logic for your requirement. I just add simple login validation only. LoginCommand will validate the login form.

LoginPageViewModel.cs

Install Fluent Validation NuGet

Install the following Nuget from Nuget Package Manager In your Visual Studio.

Fluent Validation

Create Fluent Validator

Now, Create a validator class in .Net Standard or PCL Library for your ViewModel or your business logic. I create LoginValidator for my LoginPageViewModel.

LoginValidation.cs

Here I just added the NotNull check validation with the failure message. You can explore more features for the Fluent validation

https://github.com/james-russo/XamarinForms-UnobtrusiveValidationPlugin

Result

The below image for login validation failed.

The Login validation is Success

Conclusion

I hope you have understood how to validate objects using fluent validation in MVVM in Xamarin.Forms App

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

Delpin Susai Raj Tuesday, 26 October 2021

Xamarin.Forms - How to use Fody property in MVVM

 In this blog post, you will learn how to use Fody in MVVM 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.

Fody

Fody is a library for IL weaving (the process to manipulate/inject code in IL). Fody will update automatically our code for us to add new cool features.

Fody is Injects code which raises the PropertyChanged event, into property setters of classes which implement INotifyPropertyChanged.

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 UI for LoginPage

LoginPage.xaml

Normal Property Creation with Data Binding

The Data Binding is the connection between the XAML and ViewModel of your app. You use it to connect a control (ex: text) of your UI to your logic. Let’s assume that we have an Entry and we want to link it to our ViewModel.

Following code is as we use to create the view model property be like.

LoginPage.Xaml.cs

Result

Install Fody

Install the following Nuget from Nuget Manager In your Visual Studio.

  1. Fody
  2. PropertyChanged.Fody
In your project install the Fody plugin (only in your PCL/.Net Standard project, you don’t need to install it in the Android, iOS, UWP projects

Fody NuGet

PropertyChanged.Fody

Add FodyWeavers.xml

Add FodyWeavers.xml file in your .net standard project.

Fody Properties

Now, Simply add property with { get; set; }. 

Result

Conclusion

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

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

Delpin Susai Raj Thursday, 30 September 2021

Xamarin.Forms - ContentView Lifecycle

In this blog post, you will learn how to use OnAppearing and OnDisAppearing in ContentView in Xamarin forms.

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.

ContentView LifeCycle

  • OnAppearing - raised when a View is about to appear on the screen.
  • OnDisAppearing - raised when a View is about to disappear from the screen.

Problem

In Xamarin.froms Content view or any custom view we develop. Here there are no lifecycle methods like OnStart, OnAppearing. this one is a major requirement for Xamarin.forms.

Solution

There is one workaround for OnAppearing and OnDisappearing in ContentView we can invoke the parent page OnAppearing and OnDisAppearing to our ContentView. Please follow all steps.

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.

Create Interface

Here, need to create an interface for the renderer resolver

IRendererResolver.cs

Platform Implementation

Android Implementation

Create AndroidRendererResolver in your android project. And add the following code snippets.

AndroidRendererResolver.cs

iOS Implementation

Create an iOSRenderderResolver for iOS in your iOS project. And copy-paste the following code.

IosRendererResolver.cs

Setup UI

Now, create a UI for your requirement in your ContentView.

CommonListview.xaml

ContentView LifeCycle

Following code for OnAppearing and OnDisAppearing in your ContentView. Now you can do whatever you want in your ContentView OnAppearing and OnDisAppearing.

CommonListView.xaml.cs

OnAppearing

See below screenshot of the breakpoint triggered in OnAppearing when the view is loaded.

OnDisAppearing

See below screenshot of the breakpoint triggered in OnDisAppearing when the view is unloaded.

Conclusion

I hope you have understood how to use add OnAppearing and OnDisAppearing in ContentView in Xamarin.Forms App.

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


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 :)

Delpin Susai Raj Wednesday, 17 February 2021

Xamarin.Forms - Fingerprint Authentication in your App

In this blog post, you will learn how to implement fingerprint, password and face-lock Authentication in your XamarinApp.

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.

Plugin.Fingerprint

Authenticate a user via fingerprint, face id or any other biometric / local authentication method from a cross platform API.

Prerequisites

  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project. You wíll learn more by going through the steps yourself.Create a new or existing Xamarin forms(.Net standard) Project. With Android and iOS Platform. 

Nuget

Install the following Nuget from Nuget Manager In your Visual Studio.

Plugin.Fingerprint

iOS Setup

Add NSFaceIDUsageDescription to your Info.plist to describe the reason your app uses Face ID. Otherwise the App will crash when you start a Face ID authentication on iOS 11.3+.

<key>NSFaceIDUsageDescription</key>

<string>Need your face to unlock secrets!</string>

Android Setup

Request the permission in AndroidManifest.xml

<uses-permission android:name="android.permission.USE_FINGERPRINT" />

Add below code in MainActivity.cs OnCreate Method.


Install Android X Migration

Since version 2, this plugin uses Android X. You have to install Xamarin.AndroidX.Migration in your Android project.

FingerPrint Authentication


Enable AlternativeAuthentication

If need to allow alternate authentication such as Pin, Pattern and password set AlternativeAuthentication=true

Full code

Run



I hope you have understood you will learn how to implement fingerprint, password and facelock Authentication in your XamarinApp.

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

Delpin Susai Raj Monday, 7 December 2020

Xamarin.Forms - Open App Store or Play Store in XamarinApp

 In this blog post, you will learn how to Open Play Store or App Store in XamarinApp.

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.

Prerequisites

  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

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

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

Note: Your app must be live in App store and Playstore 

Android

Here, you need the Android app id Ex:com.twitter.android.

I'm using twitter for this sample.

https://play.google.com/store/apps/details?id=com.twitter.android

Code

iOS

Here, you need AppName and AppId with location you will get appname and aped from apple developer connect.

https://apps.apple.com/in/app/twitter/id333903271

Code

Simple UI

MainPage.xaml

Full Source Code

MainPage.xaml.cs

Alternate way

You can use Lancher.OpenAsync also for opening App Store and Play store.

Using Dependency Service

Interface

iOS Implementation

Below sample code for open App store in App iOS.

Android Implementation

Below sample code for open play store in App android. 

Debug your App

I hope you have understood you will learn how to Open Play Store or App Store in XamarinApp.

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