Friday, May 27, 2011

Mango : DeviceNetworkInformation in Windows Phone 7.1

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.

Thursday, May 26, 2011

Mango : What’s new in Windows Phone 7.1

Just back from lovely Microsoft MVP Open Day 2011 at Hyderabad,Andhra Pradesh in India conducted at Microsoft India Development Center (Microsoft’s next biggest center after Redmond and some amazing people work here !) We always see and talk about success stories of Redmond people, but take a time and see and check out what an amazing contribution Indian folks doing at Microsoft IDC. Hats off ! and hearties congratulations to them and Microsoft Windows Phone 7 Team.

Group Photo @ MS IDC

[Mighty Indian MVPs at Microsoft India Development Center,India]

Whatsoever ! Mango is King of all fruits and India is the only best place in universe to get it ! Hope you can read between the lines !

Mango :

Codename “Mango” is a revolutionary and major update of Windows Phone 7 Platform.

The Windows Phone team announced the release of the Windows Phone Developer Tools for Mango yesterday. We all eagerly waiting for Mango and finally it comes to us as Beta Tools in first step.

Download Tools :

From Web :

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=77586864-ab15-40e1-bc38-713a95a56a05&displaylang=en

ISO :

http://go.microsoft.com/fwlink/?LinkID=219401

Few very important points to remember :

  • As per installation notes:Windows Server®,Windows® XP and Virtual machines are not supported.
  • Visual Basic is now fully integrated into Windows Phone Developer Tools 7.1 Beta so you must uninstall Visual Basic for Windows Phone Developer Tools 7.0 if you have installed already.
  • Windows Phone 7.1 Tools will allow you to create Windows Phone 7.0 Apps, Target platform feature like we have in Silverlight, Check this :

Platform

  • Application created with Windows Phone 7.1 will not run on NoDo Device, so hold your breath till the actual update hits the phone,Emulator is so powerful so keep your pain points aside.
  • It is not so recommended to use Windows Phone 7.1 Tools to create Windows Phone 7.0 apps for Marketplace.It may break badly.
  • Installed bits might hit Expression Blend 4, I am personally investigating this issue so not much I can share at the moment.See this what happened to my Blend 4 :

Blend4_Mess_Mango

I am running Windows 7 x64 Ultimate with Visual Studio 2010 Ultimate along with SP1, I did not uninstalled anything and seems that my Silverlight 5 Beta bits are working well, I will share more to you if I find something is breaking up. But with this info I think you are good to go.

What’s New in Mango :

Some of the top features :

  • Silverlight 4 as core platform
  • IE9 web browser control
  • Networking / sockets for communications
  • Local SQL database for structured storage
  • Live Tile enhancements: use of back of tiles and ability to
  • update Live Tiles locally
  • Deep linking into apps from notifications and Live Tiles
  • Background processing
  • New profiler and emulator for testing
  • Use of Silverlight + XNA together
  • Additional sensors; direct camera access, compass & gyro
  • Fast application switching
  • Access to calendar and contacts

There is actually a big list and instead of giving you more neck pain to scroll and read, I will suggest you to visit following link of MSDN where all new information about Windows Phone 7.1 is available.

http://msdn.microsoft.com/en-us/library/ff637516.aspx

Already downloaded tools and want to check stuff ? :

This is my first post on Mango so I will be dropping few good examples here from next post, till the time if you want you can check few cool Code samples on Windows Phone 7.1 here :

http://msdn.microsoft.com/en-us/library/ff431744%28v=VS.92%29.aspx

Some New Project Templates with Windows Phone 7.1 :

Check this out new templates as :

  • Windows Phone 3D Graphics Application
  • Windows Phone Audio Playback Agent
  • Windows Phone Audio Streaming Agent
  • Windows Phone Task Scheduler Agent

Mango_For_FB

I will soon take a stab at each one and explain it to you what each one does and when to use what template.

New Look of Windows Phone 7.1 Emulator :

The new emulator is amazing and gives feel of Mango Update on Phone, In coming days I will be surely giving more insights on that but to start with take a look how it helps you to test your location based apps and Acceleronometer apps :

1. Acceleronometer Support :

Emulator1

2. Location Support :

Emulator2

So that's all I have to say in this quick post on Mango aka Windows Phone 7.1 Beta Tools, Hope you got the glimpse of what coming in big way ahead, I will be posting few good stuff in coming days, Till the time you guys download bits and get ready,I’ll be back !

Vikram.

Sunday, May 15, 2011

Multiple Window Support in Silverlight 5

So much talks about Multiple Window support in Silverlight 5 which made available in beta version around various Silverlight blogs and forums.I am putting my few thoughts here on this feature.

The basic idea behind this feature is host stuff across multiple windows,remember the good old days of MDI forms in Visual Basic 6 days? well its not I will say replica of but its of similar kinds.

The bookish meaning of Multiple Window support is :

The ability to create an application that can have additional top level windows other than the single window that's opened for a Silverlight Out of Browser application

Game starts with namespace System.Windows.Window and it looks like following :

#region Assembly System.Windows.dll, v4.0.30319
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v5.0\System.Windows.dll
#endregion

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security;

namespace System.Windows
{
    // Summary:
    //     Represents an out-of-browser application window.
    public sealed class Window : DependencyObject
    {

        //… some code

     }

}

It’s a sealed class inherited from Dependency Object and as class level xml comment shouts, its an OOB and Work only in Elevated Trust Applications.

elevtrust

You can achieve Multiple Window like this :

Window tearOffWindow = new Window();
//Once a Window has been instantied, one can
//set its initial property values    

tearOffWindow.Height = 300;
tearOffWindow.Width = 400;
tearOffWindow.Top = 24;
tearOffWindow.Left = 30;
tearOffWindow.Title = "Tear Off Stack Panel";
tearOffWindow.Content = mnp.movUIElement; /*Set Content to someFrameworkElement Obj*/;

//Once all the initial properties have
//been set, set the Visibility property
//to 'Visible' to display the Window
tearOffWindow.Visibility = Visibility.Visible;

It will look something like this :

TearCapture

Like this you can create instance of window and put stuff on that whatever you feel appropriate to your application or per requirement. Note that once you close main window another window instance will be closed as well. Its very ideal feature for many Line of Business applications as well. So you should give try to this one today. I know there will be lot more can be done with this,but for the moment I am restricting myself,In upcoming posts I will share more with some demo apps and how you can use this feature at best.

Issues I observed with Multiple Window :

1. Background color/Background property Mystery :

While working I came across that there does not exists any Background property via code which I find strange personally.

TearOff_Error

2. “Black” Background color :

If you do not set any background color to your UIElement which you are going to tear off,It applies Black color automatically. Some videos and blogs says this is “By default” or “Automatic” but I don’t believe on this,how it can be “by default” since if I don’t apply any background color it should be whitish as usual.This I personally find very strange.

BugTearCapture

3. Setting WindowStyle as “BorderlessRoundCornersWindow” :

Though it works for parent window well,I will not say this as bug or limitation but if its not expected then they should have thrown compile time error to prompt that it cannot be mixed with Multiple Window support feature but it throws exception here  :

TearOff_Border_Error

Well, despite of these small issues which I think either might be resolve in coming bits or can be carry ahead ignoring but I feel that I should share this with you so that while designing app based on this Multiple Window feature you will be in good position with possible problems and solution.

I will soon post more deep dive article with some more good examples and features but now my mind is in Hyderabad city where 100+ South Asian MVPs are coming together for Microsoft MVP Open Day 2011 .. I will be traveling there for few days..So see you soon with more good stuff from May end onwards.

Vikram.