This tutorial is aimed just to show how to use VB.NET in WP7 development, so it will cover the following parts
-Introduction: How to create a project using WP7 tool for Vb.Net.
-Best practices: Some good practices when developing for WP7
-Example of Vb.Net code used in WP7
After having all requirements setup, go to New Project, and in New Project dialog select Silverlight for windows phone form VB part as shown below:
Number of downloads: 4
If you are new in this environment please first read an introduction part1 and part2.
When developing with WP7, there are things which need to be considered so the application will have good habit when in the phone and users can be happy when using it. The following are few things which you may need to consider:
Make it support Landscape/portrait switching:
By default, Silverlight projects come up in portrait mode and XNA come up in landscape mode. But you can also make your application to support both modes by simply changing the following line from MainPage.xaml file:
SupportedOrientations="Portrait" Orientation="Portrait"to become:
SupportedOrientations is a property of PhoneApplicationPage. It’s set to a member of the SupportedPageOrientation enumeration; Portrait, Landscape, or PortraitOrLandscape. Now user can turn his phone/emulator sideway and the application will follow the turning!
Error reporting and handling:
Developer has the responsibility to make sure he reduces the possibility of his software to crash by providing good error handling to his software. Also you can use Seesmic such that whenever there is an exception, an error messages can be sent to developer.
Some Web services would normally require the phone to frequently poll the service to obtain updated information, this can drain battery life and causes device radio being frequently turned on. To help out, a push notification service has been developed so instead of polling, a web service can notify an application of important updates as they occur.
When a web service has information to send to an application, it sends a push notification to the Push Notification Service, which in turn routes the push notification to the application. There are three types of push notifications that a web service can use when sending information to an application, tile, toast and raw.
Although these notifications are useful for the device but also should be used with care, because there is a limit of 15 notifications channels per device, and if exceeds an InvalidOperationException -Channel quota exceeded exception will be thrown. For more on push notification, look at Push Notifications Overview.
When using text box, give it an input scope:
When working with textboxes (Software Input Panels -SIP), always a default virtual (on screen) keyboard is popped up, you can customize this on-screen keyboard according to your needs. For example in my form I have three input boxes, one for username, one for phone number and the last for email address. They I can give everyone its own keyboard style, such that when I need to enter phone number only numbers are displayed.
To do this I have to give every text box its input scope, and I say:
‘so I put this code on form load Private Sub PhoneApplicationPage_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded Dim scope1, scope2 As New InputScope Dim name1, name2 As New InputScopeName ‘for name text box name1.NameValue = InputScopeNameValue.Text scope1.Names.Add(name1) TextBox1.InputScope = scope1 ‘For telephone number name2.NameValue = InputScopeNameValue.TelephoneNumber scope2.Names.Add(name2) TextBox2.InputScope = scope2 End Sub
And like so you can put the scope of the text box by selecting from InputScopeNameValue enum. Now I get the following on screen keyboard in these two different text boxes:
Number of downloads: 3 3.PNG (21.99K)
Number of downloads: 0
There are other things to care, for more look at WP7 application certification requirement.
Vb.Net examples for WP7:
From the above code, you have seen that we can use the same Vb syntax to code for windows phone. Now let’s do another example for more elaboration:
Simple Map Viewer:
In this example I am going to show you how to use Vb.Net to create a simple Bing Map Viewer using Map control available in WP7 controls.
Create a new project and add a map control in it (I will call it Map1), two buttons(zoomIn & zoomOut) and two radio-buttons(aerial & road). Then follow the following steps:
- At this time you can run application and see the map working, and you can double click on it to zoom out. But here we are going to create our own zoom in and zoom out using code
- So now go to the .vb file for the code. First of all we will deal with zoomIn and zoomOut buttons. What we really do here is to change the zoom level of the map. So in the event I will have the following:
Private Sub zoomIn_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles zoomIn.Click Dim zoomL = Map1.ZoomLevel 'get the current zoom level of the map zoomL += 1 'increment it Map1.ZoomLevel = zoomL 'change the zoom level to a new incremented one End Sub Private Sub zoomOut_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles zoomOut.Click Dim zoomL = Map1.ZoomLevel 'get the current zoom level of the map zoomL -= 1 'decrement it Map1.ZoomLevel = zoomL 'change the zoom level to a new decremented one End Sub
- Now for the radio buttons, we will change the mode of the map. The map control supports two mode types, aerial and road mode. The default one of the two is road, so make the road radio button checked by default on start up. Then we will go to the Checked event of both and do the following coding:
Private Sub road_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles road.Checked Map1.Mode = New RoadMode() 'when checked make the mode property of the map to road End Sub Private Sub aerial_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles aerial.Checked Map1.Mode = New AerialMode() 'when checked make the mode property of the map to aerial End Sub
- Now the maps is ready but let’s add something to make it cool, we add a pushpin that says “home” and try to get the position using the geo coordinate watcher(most of the time it fails in emulator).
Now including some imports and constructor the code will look like so:
Imports Microsoft.Phone.Controls Imports Microsoft.Phone.Controls.Maps Imports System.Device.Location Imports Microsoft.Phone.Controls.Maps.Platform Partial Public Class MainPage Inherits PhoneApplicationPage 'class variables Dim pp As Pushpin Dim watcher As GeoCoordinateWatcher Dim loc As GeoCoordinate ' Constructor Public Sub New() InitializeComponent() pp = New Pushpin() watcher = New GeoCoordinateWatcher(GeoPositionAccuracy.Default) watcher.TryStart(False, TimeSpan.FromMilliseconds(1000)) 'try to start the watcher with a given timeout loc = watcher.Position.Location If loc.IsUnknown <> True Then pp.Location = loc ' if the location is known assign it pp.Content = "Home" Else 'no location, just say at upper left corner pp.Content = "Location not found" End If Map1.Children.Add(pp) 'add a pushpin to the map at its location End Sub
Now when you run, we have something like:
Number of downloads: 1 aerial.PNG (139.47K)
Number of downloads: 0
And don’t worry about the message in picture, it is because I didn’t login as developer account in Bing.
Now this is an example of how to use VB.Net to code for Windows Phone Seven. Hope it will be useful for those VB.NET coders need to start WP7.
Thanks for your time to read, and any comment is appreciated