Delpin Susai Raj Thursday 5 July 2018

Xamarin.Forms- DeviceInfo Using Xamarin Essentials

In this blog you will learn how to get DeviceInfo Using Xamarin Essentials 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.

Xamarin.Essentials 

Xamarin.Essentials provides developers with cross-platform APIs for their mobile applications.
Android, iOS, and UWP offer unique operating system and platform APIs that developers have access to all in C# leveraging Xamarin. Xamarin.Essentials provides a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created.



When developing iOS and Android apps with Xamarin, developers can access every native platform API using C#. These bindings not only expose the platform APIs in C#, but add powerful C# features, such as async/await, events, delegates, and more. This is a huge advantage for developers, because they never have to leave C#, whether they’re writing shared business logic, user interface, or accessing native features. One key feature developers often look for when developing cross-platform apps with Xamarin is a way to access common native features from their shared code without having to write their own abstractions or find an open source plugin created by the community.


Xamarin.Essentials APIs

Xamarin.Essentials APIs provide access to over 25 native features from a single cross-platform API library, which can be accessed from shared code no matter how the user interface is created. This means you can use Xamarin.Essentials APIs with a single Xamarin.Android app or a Xamarin.Forms app targeting iOS, Android, and UWP. Even though it’s packed with features, it’s still fully optimized for performance and minimal impact on app size, because the library takes full advantage of being linker safe. This means only the APIs and features you use will be included in your app and the rest will be removed when you compile your app.

Platform Support

Xamarin.Essentials supports the following platforms and operating systems:

Platform     Version

Android     4.4 (API 19) or higher
iOS            10.0 or higher
UWP    10.0.16299.0 or higher

Prerequisites
  • Visual Studio 2017(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.
Choose the Xamarin.Forms App Project type under Cross-platform/App in the New Project dialog.



Name your app, select “Use Portable Class 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.



Setting up the User Interface

Go to MainPage.Xaml and write the following code.

MainPage.xaml

Add Xamarin Essentials

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





Xamarin.Essentials requires platform-specific setup

Android

Xamarin.Essentials supports a minimum Android version of 4.4, corresponding to API level 19, but the target Android version for compiling must be 8.1, corresponding to API level 27. (In Visual Studio, these two versions are set in the Project Properties dialog for the Android project, in the Android Manifest tab. In Visual Studio for Mac, they're set in the Project Options dialog for the Android project, in the Android Application tab.)

Xamarin.Essentials installs version 27.0.2 of the Xamarin.Android.Support libraries that it requires. Any other Xamarin.Android.Support libraries that your application requires should also be updated to version 27.0.2 using the NuGet package manager. All Xamarin.Android.Support libraries used by your application should be the same, and should be at least version 27.0.2. Refer to the troubleshooting page if you have issues adding the Xamarin.Essentials NuGet or updating NuGets in your solution.

In the Android project's MainLauncher or any Activity that is launched Xamarin.Essentials must be initialized in the OnCreate method:

To handle runtime permissions on Android, Xamarin.Essentials must receive any OnRequestPermissionsResult. Add the following code to all Activity classes:


iOS

No additional setup required.

UWP

No additional setup required.


In this step, write the following code for get Device Information.


MainPage.xaml.cs

using Xamarin.Forms;
using Xamarin.Essentials;
namespace XamarinEssentials
{
 public partial class MainPage : ContentPage
 {
  public MainPage()
  {
   InitializeComponent();
             
            lblDeviceName.Text ="Device Name :"+ DeviceInfo.Name;
            lblDeviceType.Text ="DeviceType :"+ DeviceInfo.DeviceType.ToString();
            lblModel.Text ="Model :"+ DeviceInfo.Model;
            lblManufacturer.Text ="Manufacturer :" + DeviceInfo.Manufacturer;
            lblPlatform.Text ="Platform : " + DeviceInfo.Platform;
            lblVersion.Text = "Version :" + DeviceInfo.Version;
  }
 }
}



Click the play button to try it out.



I hope you have understood how to get DeviceInfo Using Xamarin Essentials in Xamarin.Forms.
Thanks for reading. Please share comments and feedback.

4 comments:

  1. Nice post. I was checking continuously this blog and I’m impressed! Extremely useful info specially the last part I care for such info much. I was looking for this particular info for a very long time. Thank you and good luck.
    Hire Xamarin Developer
    Xamarin Development Company

    ReplyDelete
  2. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in Advanced Scrum Master during this lockdown period everyone can use it wisely.
    Advanced Scrum Master Training Online

    ReplyDelete
  3. The Collection Marts is platform where you can view latest designs about home décor and bedding. We have large range in different categories with finest fabric in cotton and silk. You can view not only present trends but also view huge collection with reasonable price. The Collection Marts can provide fast service about delivery as well as customer support too. Our products are not only self-made but also, well connected with markets to ensure for possibility of available designs if client want to purchase. The Collection Marts customer support open 24/7 to guide their customers about material or product stuff. cotton bed sheets online sale , bridal bed sheet set

    ReplyDelete
  4. Xamarin Monkeys: Xamarin.Forms- Deviceinfo Using Xamarin Essentials >>>>> Download Now

    >>>>> Download Full

    Xamarin Monkeys: Xamarin.Forms- Deviceinfo Using Xamarin Essentials >>>>> Download LINK

    >>>>> Download Now

    Xamarin Monkeys: Xamarin.Forms- Deviceinfo Using Xamarin Essentials >>>>> Download Full

    >>>>> Download LINK ny

    ReplyDelete