How To Bring Wifi Connectivity To Xamarin.Forms Application

Introduction
 
This article demonstrates how to bring wifi connectivity to Xamarin.Forms.
 
Let's start.
 
Step 1 
 
Open Visual Studio and go to New Project >> Installed >> Visual C# >> Cross-Platform.
 
Select Cross-Platform app, then give your project a Name and Location.

 
Step 2 
 
Add the Following NuGet Packages to Your project. 
  • Xam.Plugin.Connectivity 
Go to Solution Explorer and select your solution. Right-Click and select "Manage NuGet Packages for Solution".

 
 
Now, Select the following NuGet Package and select your project to install it.
 
 
Step  3
 
Next, add an image to Open Solution Explorer >> Project Name.Android >> Resources >> Right Click the drawable.
 
 
 
Next, a dialogue box will open. Choose image location and add images.
 
 
Step 4
 
Open Solution Explorer >> Project Name >> Mainpage.xaml. Open the Design View of this page. 
 
 
 
The Code is given below.
 
 We are Create a Lable and Image inside the StackLayout and image name is copy and paste the image source "Untitled1".
 
 
 
Xaml Code  
  1. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  2.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  3.              xmlns:local="clr-namespace:WifiConnectivity"  
  4.              x:Class="WifiConnectivity.MainPage">  
  5.     <StackLayout HorizontalOptions="Center" VerticalOptions="Center">  
  6.         <Label Text="Connect Wifi" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" FontSize="Large"/>  
  7.         <Image Source="Untitled1.jpg" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="80" WidthRequest="80"/>  
  8.     </StackLayout>  
  9.   
  10. </ContentPage>  
Step 5
 
Open Solution Explorer >> Project Name >> Right Click >> New Item.
 
 
 
The dialogue box will open. Now, add the XAML page 
 
 
 
Step 6
 
Open Solution Explorer >> Project Name >> page1.xaml. Open the Design View of this page. 
 
 
 
The code is given below 
 
 
 
Xaml Code 
  1. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  2.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  3.              x:Class="WifiConnectivity.Page1">  
  4.     <ContentPage.Content>  
  5.         <StackLayout HorizontalOptions="Center" VerticalOptions="Center">  
  6.                 <Label Text="No Wifi" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" FontSize="Large"/>  
  7.                 <Image Source="Untitled.jpg" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="80" WidthRequest="80"/>  
  8.             </StackLayout>  
  9.     </ContentPage.Content>  
  10. </ContentPage>   
Step  7
 
 Open Solution Explorer >> Project Name >> App.xaml.cs.Open the Design View of this page.
 
 
 
This Code is copied your project.
 
 
 
C# Code 
  1. using Plugin.Connectivity;  
  2. using Plugin.Connectivity.Abstractions;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Text;  
  7.   
  8. using Xamarin.Forms;  
  9.   
  10. namespace WifiConnectivity  
  11. {  
  12.     public partial class App : Application  
  13.     {  
  14.         public App ()  
  15.         {  
  16.             InitializeComponent();  
  17.   
  18.             MainPage = CrossConnectivity.Current.IsConnected ? (Page)new MainPage() : new Page1();  
  19.         }  
  20.   
  21.         protected override void OnStart ()  
  22.         {  
  23.             // Handle when your app starts  
  24.             base.OnStart();  
  25.             CrossConnectivity.Current.ConnectivityChanged += HandleConnectivityChanged;  
  26.         }  
  27.   
  28.         private void HandleConnectivityChanged(object sender, ConnectivityChangedEventArgs e)  
  29.         {  
  30.             Type CurrentPage = this.MainPage.GetType();  
  31.             if (e.IsConnected && CurrentPage != typeof(MainPage))  
  32.                 this.MainPage = new MainPage();  
  33.             else if (!e.IsConnected && CurrentPage != typeof(Page1))  
  34.                 this.MainPage = new Page1();  
  35.         }  
Step 8
 
Next, select the build & deploy option, followed the list of an Android Emulators or simulator, you can choose any API (Application Program Interface) Level Emulator and simulator to run it.
 
Output 1
 
After a few seconds, you will see your app working.
 
Wifi Connected to Mobile in the result is "Connected Wifi".
 
 

Wifi not connected to Mobile in the result is "No Wifi".
 
 
 
Finally,  We have successfully created Xamarin.Forms Wifi connectivity.


Similar Articles