Showing posts with label Cross-Platform. Show all posts
Showing posts with label Cross-Platform. Show all posts
Delpin Susai Raj Thursday, 28 November 2019

Xamarin.Forms - EventToCommand Behavior in MVVM ViewModel

In this blog post, you will learn how to use Appearing and Disappearing in MVVM ViewModel using EventToCommand Behaviour 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.

EventToCommand Behaviour

EventToCommandBehavior class is a reusable Xamarin.Forms custom behavior that executes a command in response to any event firing.

Following behavior properties must be set to use the EventToCommand behavior:
  1. EventName – the name of the event the behavior listens to(Ex: Apperaing).
  2. Command – the ICommand to be executed. The behavior expects to find the ICommand instance on the BindingContext of the attached control, which may be inherited from a parent element.
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.
Create BehaviourBase Class 

Now, Create a Behaviour Base class Derived from Behavior and BindableObject

BehavourBase.cs

Create EventToCommand Behaviour

Here, Create a EventToCommand Behaviour Class to convert your events to Commad and also implement bindable properties.
  1. EventName
  2. Command
EventToCommandBehaviour.cs

BaseViewModel

In this step, I Create a common BaseViewModel for reusable purpose.

Note: I use RelayCommand Instead of Command. you can use Command.

Refresh

Refresh command indicate to Appearing Commad. Whatever you want to implement when page load time you can use RefrehCommad.

CleanUp

Cleanup command indicate to OnDisapearing. This CleanUpCommand when your page is dis appearing time it will be called.

BaseViewModel.cs

MainPageViewModel

Now, Use Refresh and CleanUp command from BaseViewMmodel in your Pages.

MainPageViewModel.cs

Setting up the User Interface

Here, Implement EventToCommandBehaviour in your View.

Add Namespace

Add Behaviour

MainPage.Xaml 

Click the "Play" button to try it out.

I hope you have understood how to use Appearing and Disappearing in MVVM ViewModel using EventToCommand Behaviour in Xamarin.Forms..

Thanks for reading. Please share your comments and feedback. Happy Coding :)
Delpin Susai Raj Friday, 18 October 2019

Xamarin.Forms - MVVM ViewModel Locator using MVVM Light

In this blog post, you will learn how to create a MVVM ViewModel Locator using MVVM Light 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.

MVVM

MVVM - Model View ViewModel

MVVM Is the design pattern to separate the user interface & business logic concerns.I suppose that you heard something about it. This pattern created by Microsoft is widely used with applications created with .NET Framework but not only because you can also use it with Xamarin.

There are many different frameworks to help Xamarin developer
  • MVVM Cross 
  • MVVM Fresh 
  • Prism
  • MVVM Light
MVVM Light

MVVM Light lightweight framework which allows developers to choose which components they want to use. (like ViewModel Locator, Navigation Service, Dialogue Service)

Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)
  • Mvvm Light Nuget Package
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.

Visual Studio 2019 has more options in the opening window. Clone or check out the code from any repository or, open a project or solution for your computer.

Now, you need to click "Create a new project".
Now, filter by Project Type: Mobile

Choose the Mobile App (Xamarin. forms) project under C# and Mobile.

Name your app. You probably want your project and solution to use the same name as your app. Put it on your preferred location for projects and click "Create".

Now, select the blank app and target platforms - Android, iOS and Windows (UWP).

Subsequently, go to the solution. In there, you get all the files and sources of your project (.NET Standard). Now, select the XAML page and double-click to open the MainPage.Xaml page.

You now have a basic Xamarin.Forms app. Click the Play button to try it out.

NuGet Packages

Now, add the following NuGet Packages.
  • MVVM Light
  • MVVM Light Libs 
Go to Solution Explorer and select your solution. Right-click and select "Manage NuGet Packages for Solution". Search "MVVM Light" and add Package. Remember to install it for each project (.NET Standard, Android, iO).

ViewModel Locator

Here, Going to register Viewmodels and Create instance using Mvvm Light.

ViewModelLocator.cs

App.xaml.cs

In this step, Create a Viewmodellocator instance when your app first time open.

BindingContext

Now, Set your ViewModel BindingContext to ContentPage.

Done.

I hope you have understood how to create a MVVM ViewModel Locator using MVVM Light in Xamarin.Forms..

Thanks for reading. Please share your comments and feedback. Happy Coding :)
Delpin Susai Raj Wednesday, 12 June 2019

Xamarin.Forms - Change Entry Return Button

In this blog post, you will learn how to change entry return button 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.

Custom Renderers

Xamarin.Forms user interfaces are rendered using the native controls of the target platform, allowing Xamarin.Forms applications to retain the appropriate look and feel for each platform. Custom Renderers let developers override this process to customize the appearance and behaviour of Xamarin.Forms controls on each platform.

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.

Visual Studio 2019 has more options in the opening window. Clone or check out the code from any repository or, open a project or solution for your computer.

Now, you need to click "Create a new project".
Now, filter by Project Type: Mobile

Choose the Mobile App (Xamarin. forms) project under C# and Mobile.

Name your app. You probably want your project and solution to use the same name as your app. Put it on your preferred location for projects and click "Create".

Now, select the blank app and target platforms - Android, iOS and Windows (UWP).

Subsequently, go to the solution. In there, you get all the files and sources of your project (.NET Standard). Now, select the XAML page and double-click to open the MainPage.Xaml page.

You now have a basic Xamarin.Forms app. Click the Play button to try it out.

Create a Custom Entry

Now, create an Inherit class form Entry for Customising the Entry control.

Now, write the following code.

CustomEntry.cs

Android Implementation

In this step, create an inherit Class form, EntryRenderer for customizing the Entry control.

Now, write the code given below.

CustomEntryRenderer.cs

iOS Implementation

In this step, create an inherit Class form, EntryRenderer for customizing the Entry control.

Now, write the code given below.

CustomEntryRenderer.cs

Setting up the User Interface

Go to MainPage.Xaml and write the following code.

MainPage.xaml


Click the "Play" button to try it out.
I hope you have understood how to change entry return button in Xamarin.Forms..

Thanks for reading. Please share your comments and feedback. Happy Coding :)
Delpin Susai Raj Wednesday, 15 May 2019

Xamarin.Forms - Working with Material Visual

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

Material

Material Design is introduced Xamarin.Forms 3.6
Material Design is an design system created by Google, that prescribes the size, color, spacing, and other aspects of how views and layouts should look and behave.
  • Visual="Material"
  • Visual="Default"
Note:

On Android, the material renderers require a minimum version of 5.0 (API 21) or greater, and a TargetFramework of version 9.0 (API 28). In addition, your platform project requires Android support libraries 28.0.0 or greater, and its theme needs to inherit from a Material Components theme or continue to inherit from an AppCompat theme.

Material renderers are currently included the following Controls:
  1. Button
  2. Entry
  3. Frame
  4. ProgressBar
  5. DatePicker
  6. TimePicker
  7. Picker
  8. ActivityIndicator
  9. Editor
  10. Slider
  11. Stepper
More information about Material
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/visual/material-visual

Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)
Setting up a Xamarin.Forms Project

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

Visual Studio 2019 has more options in the opening window. Clone or check out the code from any repository or, open a project or solution for your computer.

Now, you need to click "Create a new project".

Now, filter by Project Type: Mobile

Choose the Mobile App (Xamarin. forms) project under C# and Mobile.

Name your app. You probably want your project and solution to use the same name as your app. Put it on your preferred location for projects and click "Create".

Now, select the blank app and target platforms - Android, iOS and Windows (UWP).

Subsequently, go to the solution. In there, you get all the files and sources of your project (.NET Standard). Now, select the XAML page and double-click to open the MainPage.Xaml page.

You now have a basic Xamarin.Forms app. Click the Play button to try it out.

NuGet Packages

Now, add the following NuGet Packages.
  • Xamarin.Forms.Visual.Material
Add Xamarin.Forms.Visual.Material NuGet

Go to Solution Explorer and select your solution. Right-click and select "Manage NuGet Packages for Solution". Search "Xamarin.Forms.Visual.Material" and add Package. Remember to install it for each project (.NET Standard, Android, iOS).

Note :

Before you initialize Xamarin.Forms in your MainActivity.cs and AppDelegate. you must Initialize FormsMaterial.Init()

Android Implementation

MainActivity.cs

iOS Implementation

AppDelegate.cs

Setting up the User Interface

In this step, added material design.

Click the "Play" button to try it out.

Android Design
iOS Design

I hope you have understood how to use Material design in Xamarin.Forms..

Thanks for reading. Please share your comments and feedback.
Happy Coding :)
Delpin Susai Raj Tuesday, 14 May 2019

Xamarin.Forms - Working with Azure Blob Storage

In this blog post, you will learn how to use Azure Blob Storage 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.

Azure Blob storage

Azure Blob storage is Microsoft's object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data.
Store any type of unstructured data—including images, videos, audio, documents and backups. you can write error log also.

Prerequisites

  • Visual Studio 2017 or later (Windows or Mac)
  • Microsft Azure subscription

Setting up a Xamarin.Forms Project

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

Visual Studio 2019 has more options in the opening window. Clone or check out the code from any repository or, open a project or solution for your computer.

Now, you need to click "Create a new project".

Now, filter by Project Type: Mobile

Choose the Mobile App (Xamarin. forms) project under C# and Mobile.

Name your app. You probably want your project and solution to use the same name as your app. Put it on your preferred location for projects and click "Create".

Now, select the blank app and target platforms - Android, iOS and Windows (UWP).

Subsequently, go to the solution. In there, you get all the files and sources of your project (.NET Standard). Now, select the XAML page and double-click to open the MainPage.Xaml page.

You now have a basic Xamarin.Forms app. Click the Play button to try it out.

Create a Storage Account in Azure

http://portal.azure.com

In this step, you must create Storage account service in azure portal.
more about storage account https://docs.microsoft.com/en-us/azure/storage/common/storage-account-overview
After creating storage account, check your access keys and connection strings.

Setting up the User Interface

Go to MainPage.Xaml and write the following code.

MainPage.xaml



NuGet Packages

Now, add the following NuGet packages.

  1. Xam.Plugin.Media
  2. Microsoft.Azure.Storage.Blob

Add Microsoft.Azure.Storage.Blob NuGet

Go to Solution Explorer and select your solution. Right-click and select "Manage NuGet Packages for Solution". Search for "Microsoft.Azure.Storage.Blob" and add the resultant package. Remember to install it for each project (.NET Standard, Android, iOS).

Connect your Storage Account

Pick Files

In this step, i'm going to upload images.




Upload Files

Now, write the following code to upload files to azure blob storage.



Download Files

Use following code to download files from azure blob storage.

Delete Files

Use following code to Delete files from azure blog storage.

Check out Full Code
MainPage.xaml.cs

Click the "Play" button to try it out.


I hope you have understood how to use Azure Blob Storage in Xamarin.Forms..

Thanks for reading. Please share your comments and feedback. Happy Coding :)
Delpin Susai Raj Friday, 10 May 2019

Xamarin.Forms - Working with Triggers

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

Triggers

Triggers allow you to express actions declaratively in XAML that change the appearance of controls based on events or property changes.

Triggers Types

  1. Property Trigger executed when a property on a control is set to a particular value.
  2. Data Trigger to trigger based on the properties of another control value.
  3. Event Trigger occurs when an event occurs on the control.
  4. Multi Trigger allows multiple trigger conditions to be set before an action occurs.

Prerequisites

  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

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

Visual Studio 2019 has more options in the opening window. Clone or check out the code from any repository or, open a project or solution for your computer.

Now, you need to click "Create a new project".

Now, filter by Project Type: Mobile

Choose the Mobile App (Xamarin. forms) project under C# and Mobile.

Name your app. You probably want your project and solution to use the same name as your app. Put it on your preferred location for projects and click "Create".

Now, select the blank app and target platforms - Android, iOS and Windows (UWP).

Subsequently, go to the solution. In there, you get all the files and sources of your project (.NET Standard). Now, select the XAML page and double-click to open the MainPage.Xaml page.

You now have a basic Xamarin.Forms app. Click the Play button to try it out.

Property Trigger

Property Trigger executed when a property on a control is set to a particular value.

Example

Click the Play button to try it out

Data Trigger

Data Trigger to trigger based on the properties of another control value.

Example

For this example the will be enabled after entering 6 digits.

Click the Play button to try it out

Event Trigger

Event Trigger occurs when an event occurs on the control.

Example

Following example, when change the text in Entry the event will fire.

Trigger Action

Include following namespace

Implement in xaml

Click the "Play" button to try it out.

Multi Trigger
Multi Trigger allows multiple trigger conditions to be set before an action occurs.

Example
The Following example, after entering two field the button will be enabled.

Click the "Play" button to try it out.
I hope you have understood how to use Triggers in Xamarin.Forms..

Thanks for reading. Please share your comments and feedback. Happy Coding :)
Delpin Susai Raj Thursday, 9 May 2019

Xamarin.Forms - CollectionView

In this blog post, you will learn how to use CollectionView instead of ListView 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.

CollectionView

CollectionView is introduce in the Xamarin.Forms 4.0 pre-releases. CollectionView is a view for presenting lists of data using different layout specifications.
It aims to provide a more flexible, and performant alternative to ListView.
  • CollectionView supports single and multiple selection.
  • CollectionView has a flexible layout model, which allows data to be presented vertically or horizontally.
Note:
  • CollectionView is only available on iOS and Android.
More information about Collection View
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/

Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)
Setting up a Xamarin.Forms Project

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

Visual Studio 2019 has more options in the opening window. Clone or check out the code from any repository or, open a project or solution for your computer.

Now, you need to click "Create a new project".

Now, filter by Project Type: Mobile

Choose the Mobile App (Xamarin. forms) project under C# and Mobile.

Name your app. You probably want your project and solution to use the same name as your app. Put it on your preferred location for projects and click "Create".

Now, select the blank app and target platforms - Android, iOS and Windows (UWP).

Subsequently, go to the solution. In there, you get all the files and sources of your project (.NET Standard). Now, select the XAML page and double-click to open the MainPage.Xaml page.

You now have a basic Xamarin.Forms app. Click the Play button to try it out.

Note: 

Before you initialize Xamarin.Forms in your MainActivity.cs and AppDelegate. Add following Flag

Android Implementation

MainActivity.cs

iOS Implementation

AppDelegate.cs

Setting up the User Interface

Now, Implement the collectionview with data binding.

CollectionView ItemSource

Now, add ItemSource to CollectionView

Click the "Play" button to try it out.

I hope you have understood how to use CollectionView instead of ListView in Xamarin.Forms..

Thanks for reading. Please share your comments and feedback. Happy Coding :)
Delpin Susai Raj Friday, 16 November 2018

Xamarin.Forms - Error Log in Visual Studio App Center

In this blog post, you will learn how to collect Error log Using Visual Studio App Center 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.

Visual Studio App Center



App Center have multiple services, that are most commonly used by mobile developers, where multiple services are acts as a single integrated product. With the use of single integrated product you can build, test, distribute, and monitor your mobile apps, and also implement push notifications.

Support Platforms
  1. Android
  2. iOS
  3. React Native
  4. UWP
  5. Xamarin
  6. macOS
  7. Cordova

App Center Services
  • Build
  • Diagnostics (Formerly Crashes)
  • Test
  • Analytics
  • Distribute
  • Push Notifications



Errors

Errors will happens when a runtime exception occurs from an unexpected event, this errors will not terminates the app. These are the errors handled by a try/catch block. When a Error occurs, App Center will records the details of the error, then the state of the app and device are automatically generates a error log based on the records. These logs contains valuable information about the error and it will help you out to fix the error.


Prerequisites
  • Visual Studio 2017(Windows or Mac)
  • Visual Studio App Center Account
Setting up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project.  you’ll learn more by going through the steps yourself.
Choose the Xamarin.Forms App Project type under Cross-platform/App in the New Project dialog.



Name your app, select “Use Shared Library” for shared code, and target both Android and iOS.



You probably want your project and solution to use the same name as your app. Put it in your preferred folder for projects and click Create.



You now have a basic Xamarin.Forms app. Click the play button to try it out.



Create a app in App Center(iOS)

In this step, create a app in App Center. Go to the following link.

https://appcenter.ms/

Now, sign in using your preferred account.



Add New app.

In this step, give your app Name (Ex:MyApp) and description.

OS           :iOS
Platform  :Xamarin

Afterwards, click Add New app.



Now, your app center app is ready. You can use it now.



Setting up the User Interface

Go to MainPage.Xaml and write the following code.

MainPage.xaml

Click the play button to try it out.



Add AppCenter Crashes NuGet

In this step, add AppCenter Crashes to your project. You can install Microsoft.AppCenter.Crashes 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 "Microsoft.AppCenter.Crashes" and add Package. Remember to install it for each project (PCL, Android, iO, and UWP).



AppCenter.Crashes requires platform-specific setup

Xamarin.forms PCL

The following steps are necessary for PCL.

In the PCL project's App.xaml.cs that is launched AppCenter.Crashes must be initialized in the OnStart() method.

App.xaml.cs


iOS

The following steps are necessary for iOS.

In the iOS project's AppDelegate that is launched AppCenter.Crashes must be initialized in the FinishedLaunching() method.

AppDelegate.cs

Android

You need to create another app in App Center with os option Android.
https://appcenter.ms/

UWP

You need to create another app in App Center with os option Winodws.
https://appcenter.ms/

Error

In this step, write the following code for simple exception.

MainPage.xaml.cs

Click the play button to try it out.



Errors



Error Details



I hope you have understood how to collect Error log Using Visual Studio App Center in Xamarin.Forms.

Thanks for reading. Please share comments and feedback.