Reading this article, you will learn how to check Network Connectivity in Xamarin Forms application for Android and Universal Windows Platform with XAML and Visual C# in cross platform application development.
The following important tools are required for developing UWP,
- Windows 10 (Recommended)
- Visual Studio 2017 RC Community/Enterprise/Professional Edition (It is a Free trial software available online)
- Using Visual studio 2017 Installer, Enable the feature of Mobile development with .NET.
Now we can discuss step by step App development.
Step 1
Step 2
Select the Cross Platform template as Blank APP ->Set UI Technology as Forms and Sharing as PCL and Afterwards, Visual Studio creates 4 projects (Portable, Droid, iOS, UWP) and displays Getting Started.XamarinPage,
Step 3
Add reference to all the Projects
Step 4
- <StackLayout>
- <Label Text="Xamarin Forms - Check Network Connectivity in UWP and Android " VerticalOptions="Center" HorizontalOptions="Center" />
- <Label Text="Connected Network is !....." TranslationX="0" TranslationY="100" VerticalOptions="Center" HorizontalOptions="Center" />
- <Label x:Name="ConNet" TranslationX="0" TranslationY="100" VerticalOptions="Center" HorizontalOptions="Center" FontSize="Large" />
- </StackLayout>
Step 5
- using Xamarin.Forms;
- using Plugin.Connectivity;
- using Plugin.Connectivity.Abstractions;
- protected override void OnStart() {
- // Handle when your app starts
- CrossConnectivity.Current.ConnectivityChanged += HandleConnectivityChanged;
- }
- void HandleConnectivityChanged(object sender, ConnectivityChangedEventArgs e) {
- Type currentPage = this.MainPage.GetType();
- if (e.IsConnected && currentPage != typeof(MainPage)) this.MainPage = new MainPage();
- else if (!e.IsConnected && currentPage != typeof(MainPage)) this.MainPage = new MainPage();
- }
Step 6
- using Xamarin.Forms;
- using Plugin.Connectivity;
- using Plugin.Connectivity.Abstractions;
- protected override void OnAppearing() {
- ConNet.Text = CrossConnectivity.Current.ConnectionTypes.First().ToString();
- CrossConnectivity.Current.ConnectivityChanged += UpdateNetworkInfo;
- }
- protected override void OnDisappearing() {
- CrossConnectivity.Current.ConnectivityChanged -= UpdateNetworkInfo;
- }
- private void UpdateNetworkInfo(object sender, ConnectivityChangedEventArgs e) {
- var connectionType = CrossConnectivity.Current.ConnectionTypes.FirstOrDefault();
- ConNet.Text = connectionType.ToString();
- }
Step 7
Change the Configuration Manager settings, Go to Build -> Configuration Manager,
Uncheck the Build and deploy options to the iOS and Check the Droid and UWP
Step 9
Summary
Now you have successfully tested Network connectivity in Xamarin Forms application for Cross Platform Application Development using Visual C# and Xamarin.

Sachi CPosted Feb 21, 2018, 3:05 AM
I installed the reference but its namespace not detecting in VS 2017 app.xaml.cs
Guest UserPosted Mar 12, 2017, 2:47 PM
Starting to setup my Dev environment. When finish, I will try your instructions...They look great!
Ravi KandelPosted Mar 8, 2017, 8:26 AM
Awesome Thanks for sharing