The Connectivity class is available at Xamarin.Essentials API. It is used to monitor for changes in the device's network conditions, check the current network access, and how it is currently connected. Android, iOS, and UWP offer unique operating systems 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.
Reading this article, you can learn how to check the changes of network connectivity conditions and current network access in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform with XAML and Visual C# in the cross-platform application development.
The following important tools are required for developing Xamarin Forms App,
- Windows 10 (Recommended)
- Visual Studio 2017
- Android API 19 or higher and UWP 10.0.16299.0 or higher.
Now, we can discuss step by step app development.
Step 1
Open Visual Studio 2017 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Mobile App (Xamarin.Forms)-> Give a suitable name for your app (XamFormConnectivity) ->OK
Step 2
Select the Cross-Platform template as a Blank APP ->Set platform as Android and UWP and code sharing strategy as .NET standard. Afterwards, Visual Studio creates 3 projects (Portable, Android, UWP)
Step 3
For adding a reference,
RightClick Your solution (XamFormConnectivity) and Select Manage NuGet Packages. For adding Xamarin.Essentials reference, choose Browse and search Xamarin.Essentials, select the package and select all the projects (portable, Android, UWP) and install it.
Step 4
Add the Label controls in Mainpage.Xaml for displaying Title and Netwok Access, Network Profile.
- <Label FontAttributes="Bold" Text="Check the Network Condition changes in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center"/>
- <Label HorizontalOptions="Center" x:Name="lblNetStatus"></Label>
- <Label HorizontalOptions="Center" x:Name="lblNetProfile"></Label>
Step 5
Add the following code in theAssemblyInfo.csin XamFormConnectivity.Android project under Properties folder,
- [assembly: UsesPermission(Android.Manifest.Permission.BatteryStats)]
Step 6
Add the following code in the MainActivity.cs in XamFormConnectivity.Android project,
In onCreate() method,
- Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Also add the below method,
- publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) {
- Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- }
Step 7
Add the following namespace and code in Mainpage.Xaml.cs
- using Xamarin.Essentials;
- public MainPage() {
- InitializeComponent();
- ConnectivityTest();
- }
- publicvoid ConnectivityTest() {
- Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
- }
- void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e) {
- var access = e.NetworkAccess;
- var profiles = e.Profiles;
- if (access == NetworkAccess.Internet) {
- lblNetStatus.Text = "Local and internet access";
- }
- elseif(access == NetworkAccess.ConstrainedInternet) {
- lblNetStatus.Text = "Limited internet access";
- }
- elseif(access == NetworkAccess.Local) {
- lblNetStatus.Text = "Local network access only";
- }
- elseif(access == NetworkAccess.None) {
- lblNetStatus.Text = "No connectivity is available";
- }
- elseif(access == NetworkAccess.Unknown) {
- lblNetStatus.Text = "Unable to determine internet connectivity";
- }
- if (profiles.Contains(ConnectionProfile.WiFi)) {
- lblNetProfile.Text = profiles.FirstOrDefault().ToString();
- } else {
- lblNetProfile.Text = profiles.FirstOrDefault().ToString();
- }
- }
Note
Automatically the following code will be generated in XAML code view, while we are done in the design view.
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamFormConnectivity" x:Class="XamFormConnectivity.MainPage">
- <StackLayout>
- <Label FontAttributes="Bold" Text="Check the Network Condition changes in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />
- <Label HorizontalOptions="Center" x:Name="lblNetStatus"></Label>
- <Label HorizontalOptions="Center" x:Name="lblNetProfile"></Label>
- </StackLayout>
- </ContentPage>
Step 8
Deploy your App in UWP and Android. When wifi connectivity is enabled in UWP, the output of the XamFormConnectivity App is:
When mobile data is enabled in Android,
When the wifi connectivity is disabled in UWP,
When mobile data is enabled in Android,
Summary
Now, you have successfully checked the device network changes in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform using Visual C# and Xamarin.