Window Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 27 May 2011

Mango : DeviceNetworkInformation in Windows Phone 7.1

Posted on 11:05 by Unknown

Hope you already downloaded Windows Phone 7.1 Mango Beta Tools as I mentioned in my last post here

Today I am going to talk about DeviceNetworkInformation API with small demo. This API is exposed with the new toolset and very useful especially when you need to check various things related to Mobile Operator. Let’s have a look in more depth about this API.

What this API does ? :

This API helps you to identify and know more about Network capabilities by which one can identify Availability of Network, Availability of Roaming Data and WiFi Network etc. How to make use of this in your Windows Phone 7.1 app its totally your call and it is dependent on your Application Requirement. So I will leave this “Where to use” part to you.

Structure of DeviceNetworkInformation API :

DeviceNetworkInformation is a static class insider Microsoft.Phone.Net.NetworkInformation namespace having few boolean properties and ResolveHostNameAsync method and NetworkAvailabilityChanged. Since all the properties are static you don’t need to create instance. Properties are

  • CellularMobileOperator
  • IsCellularDataEnabled
  • IsCellularDataRoamingEnabled
  • IsNetworkAvailable
  • IsWiFiEnabled

It resides in Microsoft.Phone.dll(v2.0.50727) which you can find in WindowsPhone71 directory.

General Structure is like this :

namespace Microsoft.Phone.Net.NetworkInformation
{   
    [SecuritySafeCritical]
    public static class DeviceNetworkInformation
    {
        public static string CellularMobileOperator { get; }
        public static bool IsCellularDataEnabled { get; }    
        public static bool IsCellularDataRoamingEnabled { get; }     
        public static bool IsNetworkAvailable { get; }      
        public static bool IsWiFiEnabled { get; }

        public static event EventHandler<NetworkNotificationEventArgs> NetworkAvailabilityChanged;

        public static void ResolveHostNameAsync(DnsEndPoint endPoint, NameResolutionCallback callback, object context);
        public static void ResolveHostNameAsync(DnsEndPoint endPoint, NetworkInterfaceInfo networkInterface, NameResolutionCallback callback, object context);
    }
}
Now Let’s build one small App on top of this API, So here are steps :

How it will look like :

NetworkInfo

*Please note that Images used in this post are from Internet found during regular search, Images have their respective copyrights and they are part of this post just for demo purpose.

XAML :

<!--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="Explore .NET With Vikram Pendse" 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">
            <Image Source="GSMNetwork.jpg" Opacity="0.3" Height="607" HorizontalAlignment="Left" Margin="-2,-6,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="458" />
            <TextBlock FontWeight="Bold" x:Name="txtMobileOptr" Height="30" HorizontalAlignment="Left" Margin="0,104,0,0" VerticalAlignment="Top" Width="450" />
            <TextBlock FontWeight="Bold" x:Name="txtNetworkAvail" Height="30" HorizontalAlignment="Left" Margin="0,158,0,0" VerticalAlignment="Top" Width="450" />
            <TextBlock FontWeight="Bold" x:Name="txtCDEnable" Height="30" HorizontalAlignment="Left" Margin="0,212,0,0" VerticalAlignment="Top" Width="450" />
            <TextBlock FontWeight="Bold" x:Name="txtRoaming" Height="30" HorizontalAlignment="Left" Margin="2,269,0,0" VerticalAlignment="Top" Width="448" />
            <TextBlock FontWeight="Bold" x:Name="txtWiFi" Height="56" TextWrapping="Wrap" HorizontalAlignment="Left" Margin="0,325,0,0" VerticalAlignment="Top" Width="450" />
            <Image x:Name="imgWiFi" Height="150" HorizontalAlignment="Left" Margin="228,431,0,0" Stretch="Fill" VerticalAlignment="Top" Width="200" />
        </Grid>

So on designer layout in Visual Studio it will look like this :

NetworkDesign

C# Code :

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
     GetDeviceNetworkInformation();
}

public void GetDeviceNetworkInformation()
{
     txtMobileOptr.Text = "Your Mobile Operator is : " + DeviceNetworkInformation.CellularMobileOperator.ToString();
            
     if (DeviceNetworkInformation.IsNetworkAvailable)
        txtNetworkAvail.Text = "Network is Available";
     else
        txtNetworkAvail.Text = "Network is Not Available";

// Similarly you can get and set other properties to Textblocks

}

Output :

NetworkInfo

NetworkZoom

Now if you need to make it more Jazzy you can put icons and make it more attractive and friendly. For example, I have done for Wi Fi, I am showing Wi Fi enable – disable by displaying PNG file like this :

NetworkWiFiEnable          NetworkWiFiDisable

I know the images used here are bit dull,but idea is to show how you can enhance the app by such ideas and code for this is very simple like this :

if (DeviceNetworkInformation.IsWiFiEnabled)
{
    txtWiFi.Text = "You are connected to WiFi Network";
    Uri uri = new Uri(@"wificon.png", UriKind.Relative);
    ImageSource imgSource = new BitmapImage(uri);
    imgWiFi.Source = imgSource;
}
else
{
    txtWiFi.Text = "You are not connected to WiFi Network,Check your WiFi Settings and Retry";
    Uri uri = new Uri(@"wifidiscon.png", UriKind.Relative);
    ImageSource imgSource = new BitmapImage(uri);
    imgWiFi.Source = imgSource;
}

So this is how you can make use of DeviceNetworkInformation in your Windows Phone 7.1 Application, As I mentioned earlier that how you will use this is totally your choice and depends on business scenario, But next time when you need all Network related information inside your app, you don’t need to search, The solution is already with you with this API. See you then with more good stuff on Mango aka Windows Phone 7.1 in coming posts.Keep buzzing over emails and in comment section as usual.

Vikram.

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • First Windows Phone 7 update February 2011 - Small update but Big start
    After tons of rumors and set of predictions on Windows Phone 7 all over Internet, Microsoft came up with first Windows Phone 7 minor update ...
  • The little Story of “I Unlock Joy” event by Microsoft and Pune User Group
      This post is about recent “I Unlock Joy” event happened in Pune which was conducted by Microsoft and Pune User Group. Little History : ...
  • Silverlight On Mobile : Windows Phone 7 Splash Screen and Customization
    After talking about 3D capabilities on Windows Phone 7 using Silverlight in last article , Now I am moving ahead with small but equally impo...
  • Silverlight 5 : Platform Invoke (PInvoke) in Silverlight
      Two days back Microsoft announced availability of Silverlight 5 RC,I encourage you to download bits from here , My friend Pete Brown alr...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 1
    After a long..I am writing blog, I hope and I wish I will resume blogging like I use to in past. Lots of things happened in past few months....
  • MCTS : Microsoft Silverlight 4 Development Exam Guide (70-506) by Packt Publishing
      Hello, After a long time I got chance to come back here.I will soon resume blogging in month of August. Last 4-5 months were horrible due...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 2
    Hope you enjoyed my last article on Speech Capability in Windows Phone 8, Today I am posting another part or you can say little extension t...
  • Silverlight 3 : Insert & Update Data using WCF Service with DataForm and DataGrid
    In my Lap around Silverlight 3 series, I have written a separate article on DataForm in Silverlight 3, This article is a basic extension to ...
  • Mango : Using DeviceStatus in Windows Phone 7.1
    First of all “Thank You” for your wonderful response and comments on my last article on Silverlight Vs HTML5 ,I hope you like the points I ...
  • Silverlight, HTML5 & Windows 8 : Where we are heading to ?
    This is not the post or yet another post on most happening debate of Silverlight and HTML5, This is just a visit to all of them to realize t...

Blog Archive

  • ►  2013 (4)
    • ►  August (1)
    • ►  April (3)
  • ►  2012 (4)
    • ►  July (1)
    • ►  March (2)
    • ►  January (1)
  • ▼  2011 (24)
    • ►  December (1)
    • ►  September (4)
    • ►  August (2)
    • ►  July (1)
    • ►  June (4)
    • ▼  May (3)
      • Mango : DeviceNetworkInformation in Windows Phone 7.1
      • Mango : What’s new in Windows Phone 7.1
      • Multiple Window Support in Silverlight 5
    • ►  April (3)
    • ►  March (1)
    • ►  February (4)
    • ►  January (1)
  • ►  2010 (21)
    • ►  December (1)
    • ►  November (2)
    • ►  October (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (5)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  January (1)
  • ►  2009 (49)
    • ►  December (1)
    • ►  November (5)
    • ►  October (2)
    • ►  September (1)
    • ►  August (5)
    • ►  July (5)
    • ►  June (1)
    • ►  May (5)
    • ►  April (5)
    • ►  March (9)
    • ►  February (4)
    • ►  January (6)
  • ►  2008 (43)
    • ►  December (3)
    • ►  November (9)
    • ►  October (7)
    • ►  September (4)
    • ►  August (2)
    • ►  July (3)
    • ►  June (4)
    • ►  May (3)
    • ►  March (3)
    • ►  February (5)
Powered by Blogger.

About Me

Unknown
View my complete profile