Introduction
This article demonstrates how to bring internet 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
- Xamarin.Forms
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.
Similarly, add the Xamarin.Forms Nuget.
Step 3
Open Solution Explorer >> Project Name >> Mainpage.xaml. Open the Design View of this page.
The code is given below.
XAML Code
- <Button x:Name="Btn" Text="Check Connectivity" Clicked="Btn_Clicked"/>
- <Label x:Name="Lbl" Text="Status"/>
- <Label x:Name="Lbl1" Text="Type:/"/>
Step 4
Open Solution Explorer >> Project Name >> Mainpage.xaml.cs Open the Design View of this page.
C# Code
- namespace ConnectivityCheck
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- }
- private void Btn_Clicked(object sender, EventArgs e)
- {
- var Status = CrossConnectivity.Current.IsConnected ? "Is Connected" : "Not Connected";
- Lbl.Text = $"Status :{Status}";
- Lbl1.Text = $"Type:{CrossConnectivity.Current.ConnectionTypes.ToString() }";
- }
- }
- }
Step 5
Press F5 or Build and Run the application.
OutPut 1
Mobile Data On will result in "Connected" status.
Output 2
Mobile Data Off will result in NotConnected status.
Finally, we have successfully created a Xamarin.Forms InternetConnectivity app.