Floating FB popout byF5debug

101 How to on Windows Phone – How to #45 – Programmatically determine the Network information in Windows Phone

 

In this article we are going to see how to use the network capabilities of a Windows Phone programmatically. While we develop application we might come across requirement to understand the network capabilities and then do some process which can be done with windows phone development using the DeviceNetworkInformation Class. This class can be used directly with out creating any instance of the class as they are static, so we can directly use the properties of the class in the application. DeviceNetworkInformation class the below set of properties.       [more]

  • IsNetworkAvailable
  • IsCellularDataEnabled
  • IsCellularDataRoamingEnabled
  • IsWifiEnabled
  • CellularMobileOperator

So lets see how we can use the network capabilities in our application by simply writing some of the base code which gathers the information about the network. Let us see the step by step process on how to use network information in real time developing the Windows phone 7 application. Open Visual Studio 2012 and create a new Silverlight for Windows Phone 7 project with a valid project name as shown in the screen below.

image


Once the project is initialized, we can see a list of default files and folders created. Now in the Mainpage.xaml let us add some controls which are used to show the network information. So the XAML code looks like below which has a Button control to trigger the event on demand, and a textbox control to show the details as shown in the screen below.

image

 

Code:

[code:c#]

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="F5debug How to Series" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Network Info" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Content="Get NetworkInfo" Height="72" HorizontalAlignment="Left" Margin="9,6,0,0" Name="button1" VerticalAlignment="Top" Width="441" />
            <TextBlock Height="277" HorizontalAlignment="Left" TextWrapping="Wrap" Margin="29,108,0,0" Name="textBlock1" Text="" VerticalAlignment="Top" Width="407" />
        </Grid>

[/code]


Now to get the network information we need to use the DeviceNetworkInformation class, to start with import the below code in the using statement in the code base at the top of the page.

Code:

[code:c#]

using Microsoft.Phone.Net.NetworkInformation;
 

[/code]

Now next step is in the button click event write the below code which gets the network information with the help of the properties that is already available with the DeviceNetworkInformation class that we imported at the beginning of the code base as shown in the code below.

[code:c#]

private void button1_Click(object sender, RoutedEventArgs e)
        {
            System.Text.StringBuilder sbInformation = new System.Text.StringBuilder();

            sbInformation.Append("Network available:  ");
            sbInformation.AppendLine(DeviceNetworkInformation.IsNetworkAvailable.ToString());

            sbInformation.Append("Cellular enabled:  ");
            sbInformation.AppendLine(DeviceNetworkInformation.IsCellularDataEnabled.ToString());

            sbInformation.Append("Roaming enabled:  ");
            sbInformation.AppendLine(DeviceNetworkInformation.IsCellularDataRoamingEnabled.ToString());

            sbInformation.Append("Wi-Fi enabled:  ");
            sbInformation.AppendLine(DeviceNetworkInformation.IsWiFiEnabled.ToString());           

            textBlock1.Text = sbInformation.ToString();
        }

[/code]

Now we are done with our code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs as shown in the screens below.

image

 

Complete Code:

XAML Code:

[code:c#]

<phone:PhoneApplicationPage
    x:Class="F5debugHowTo45.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="F5debug How to Series" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Network Info" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Content="Get NetworkInfo" Height="72" HorizontalAlignment="Left" Margin="9,6,0,0" Name="button1" VerticalAlignment="Top" Width="441" Click="button1_Click" />
            <TextBlock Height="277" HorizontalAlignment="Left" TextWrapping="Wrap" Margin="29,108,0,0" Name="textBlock1" Text="" VerticalAlignment="Top" Width="407" />
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

[/code]

C# Code:

[code:c#]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using F5debugHowTo45.Resources;
using Microsoft.Phone.Net.NetworkInformation;

namespace F5debugHowTo45
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            System.Text.StringBuilder sbInformation = new System.Text.StringBuilder();           

            sbInformation.Append("Network available:  ");
            sbInformation.AppendLine(DeviceNetworkInformation.IsNetworkAvailable.ToString());

            sbInformation.Append("Cellular enabled:  ");
            sbInformation.AppendLine(DeviceNetworkInformation.IsCellularDataEnabled.ToString());

            sbInformation.Append("Roaming enabled:  ");
            sbInformation.AppendLine(DeviceNetworkInformation.IsCellularDataRoamingEnabled.ToString());

            sbInformation.Append("Wi-Fi enabled:  ");
            sbInformation.AppendLine(DeviceNetworkInformation.IsWiFiEnabled.ToString());
 

            textBlock1.Text = sbInformation.ToString();
        }
    }
}

[/code]

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter