Windows Phone Camp coming to Pune and Chennai on 20th and 22nd December

I am excited Windows Phone camp is coming to Pune and Chennai. I will be there to learn latest of Windows Phone development from the experts. Are you joining me? I want to promote myself from novice Windows Phone developer to a professional developer. I have many queries to be clarified and Windows Phone camp is great opportunity for me to be ahead in the curve.

Details of Pune event
Details of Chennai event

image

Who can attend?

  • Are you a novice Windows Phone Developer and want to be Professional Windows Phone developer?
  • Are you aspired to be a Windows Phone Developer?
  • Are you interested to find potential of Windows Phone, Marketplace and business revolves around Windows Phone?
  • Are you an iPhone, Android developer and want to have a date with beauty of Windows Phone?

If you find yourself in any of the above category then Windows phone camp is for you.

Details of Events

Pune Event Details

When: 20th December 2011

Where: Le Meridian, Raja Bahadur Mill Road, Pune

Charge: Free

Register: here

Chennai Event Details

When: 22nd December 2011

Where: Savera Hotel, 146, Dr. Radhakrishnan Road, Chennai

Charge: Free

Register: here

See you in Pune Windows Phone camp.

How to check application returning from dormant state in Windows Phone 7

What is Fast Application Switching (FAS)?

Imagine application “A” is running and user launches a new application “B”. On pressing hardware back button user can return back to application “A”. If application “A” gets activated without “resuming “ screen then it can be termed as “ Fast Application Switching

What is dormant state?

In Windows Phone 7.5 version FAS is improved over Windows Phone 7 with introduction of new state in application life cycle called “Dormant state”. Whereas in Windows Phone 7.0 version there was no Dormant state and on deactivation application directly goes to Tombstone state.

With introduction of dormant state in application life cycle, operating system preserve instance of the application in memory unless it is forced to release due to low memory. So at the time of application reactivation as a developer you need to check whether to restore the application or not? If application is reactivating from dormant state then there should not be restoration.

Checking Application returning from Dormant state

On Application Activated event you can check whether application is reactivating from dormant or tombstone state as below,


private void Application_Activated(object sender, ActivatedEventArgs e)
{

if (e.IsApplicationInstancePreserved)
{

// No need to restore the application.
// Application is reactivating from dormant state


}

else
{
// Need to restore the application.
// Application is reactivating from tombstone state
// Read state dictionary and Navigation state to restore memory state

}
}


In this way you can check whether application is returning from dormant state. I hope this post is useful. Thanks for reading.

How to work with ToggleSwitch in Windows Phone 7

In this post I will discuss Toggle Switch Button. This comes as part of Silverlight toolkit for Windows Phone 7 and you get it from here

imageimage

To work with Toggle Switch, you need to add namespace


xmlns:tool="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

 

 

You can use ToggleSwitch as below,

 


<tool:ToggleSwitch x:Name="tglSwitch"
Header="wifi"
Checked="tglSwitch_Checked"
Unchecked="tglSwitch_Unchecked"/>


You can set Header and Content of the Toggleswitch. If you want you can very much templateaize Header and Content.

There are four events attached with Toggle Switch.

image

You can handle events as below,

void tglSwitch_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Its Clicked");

}

void tglSwitch_Indeterminate(object sender, RoutedEventArgs e)
{
MessageBox.Show("Its intermidiate");
}

private void tglSwitch_Checked(object sender, RoutedEventArgs e)
{
tglSwitch.Content = "on";
}

private void tglSwitch_Unchecked(object sender, RoutedEventArgs e)
{
tglSwitch.Content = "off";
}


In this way you can work with ToggleSwitch . I hope this post is useful. Thanks for reading.

Calling WCF REST Service with Basic authentication from Windows Phone 7

Today while working I came across a requirement to consume a REST Service. To consume REST Service application had to provide username and password. Initially I thought it should be easier task and I tried consuming REST Service as below,


WebClient proxy = new WebClient();
NetworkCredential credential = new NetworkCredential("username", "password");
proxy.Credentials = credential;
proxy.DownloadStringCompleted += new DownloadStringCompletedEventHandler(proxy_DownloadStringCompleted);
proxy.DownloadStringAsync(new Uri("http://localhost:2572/Service1.svc/GetStudents"));

In completed event accessing returned data as below,


void proxy_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string result = e.Result;
MessageBox.Show(result);

}

When I tried running application I got below exception

image

WebClient is the main reason behind above exception. WebClient is unable to pass credential to the service. Better approach to call REST Service with Basic authentication is to use HttpWebRequest class.


HttpWebRequest client = (WebRequest.CreateHttp(new Uri("http://localhost:2572/Service1.svc/GetStudents"))) as HttpWebRequest;
NetworkCredential cred = new NetworkCredential("username", "password");
client.Credentials = cred;
var result = (IAsyncResult)client.BeginGetResponse(ResponseCallback, client);
MessageBox.Show(responseData);

And in Responsecallback method parse result as below. In this case I am calling the service on different thread.


private void ResponseCallback(IAsyncResult result)
{
var request = (HttpWebRequest)result.AsyncState;
var response = request.EndGetResponse(result);
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{

var contents = reader.ReadToEnd();
Dispatcher.BeginInvoke(() => { responseData = contents; });
}
}

You should be using HttpWebRequest to consume the REST Service with Basic authentication. I hope this post is useful. Thanks for reading.

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

What are Launchers and Choosers in Windows Phone 7

Launcher and Choosers API allows application to interact with Native Applications of Windows Phone 7

clip_image002

Launchers

clip_image003

  • Launchers are collection of classes in Windows Phone 7.
  • They help to interact with inbuilt applications on native application of Windows Phone 7.
  • Launchers used to launch one of the inbuilt or native applications from your application.
  • Launchers do not return any data from the inbuilt or native application.

Different Launchers are as below,

  • EmailComposerTask
  • PhoneCallTask
  • SmsComposeTask
  • MarketPlaceHubTask
  • MarketPlaceSearchTask
  • MarketPlaceDetailsTask
  • MarketPlaceReviewTask
  • MediaPlayerLauncher
  • WebBrowserTask
  • SearchTask

Choosers

clip_image004

  • Choosers are collection of classes in Windows Phone 7.
  • They help to interact with inbuilt or native applications of Windows Phone 7.
  • Choosers launch and native application and returns data from the launched application for further manipulation also.
  • Choosers return data to the application using choosers.
  • It’s not necessary that all the Choosers return data. For example SaveEmailAddressTask chooser task does not return any data to the calling application.

Data return by the Choosers should be handled in the completed event of the Choosers.

Different choosers are as below,

  • CameraCaptureTask
  • PhotoChoosersTask
  • SaveEmailAddressTask
  • EmailAddressChoosersTask
  • SavePhoneNumberTask
  • PhoneNumberChooserTask

Using Choosers and Launchers in Code

You need to make instance of choosers or launcher to use them. You need to add below namespace to use various launchers or choosers.

clip_image005

After creating instance you need to call Show method () as below. There are many options also you can set with different launchers.

clip_image006

In case of Chooser you need to handle completed event to work with returned data.

Good Practice to use Choosers in the code

There are three points you need to keep in mind while working with choosers in Windows Phone 7. To explain you more I am using PhotoChooserTask

Three points are as below,

Point 1

You need to instantiate Choosers as the global variable on the page of the application

clip_image007

Point 2

You need to make sure that you are registering completed event in constructor of the page.

clip_image009

Point 3

In the completed event of Chooser, first you need to check for the TaskResult value. If it is ok then perform the task.

clip_image010

This is all about Launchers and Choosers. I hope this post is useful. Thanks for reading.

How to create Splash Screen with Progress Bar in Windows Phone 7

Idea behind implementation

To create SplashScreen with ProgressBar idea is to put a popup visible till start page is ready.

image

  • Open user control in popup
  • Cut Splash Screen image with dimension 800×460 from the popup
  • Make visible popup till start page of the application is not ready

Create User Control

Very first you need to create UserControl for the Splash Screen with Progress Bar. In the UserControl, let us put below three controls in Grid.

  • Image Control to display Image
  • TextBlock to display text
  • ProgressBar to display progress status.

I am putting above controls in Grid with proper margin to display them vertically. XAML of UserControl is as below,

<Grid x:Name="LayoutRoot" Height="800" Width="640">
<Image Source="waitsymbol.jpg"
Margin="0,0,0,0"
VerticalAlignment="Top"
Height="650"
HorizontalAlignment="Left"
Width="400"
Stretch="Fill"/>
<TextBlock
Text="loading...."
HorizontalAlignment="Left"
Style="{StaticResource PhoneTextTitle2Style}"
Margin="185,656,0,97"
/>
<ProgressBar x:Name="SpalshScreenProgressbar"
Width="380"
HorizontalAlignment="Left"
IsIndeterminate="True"
Margin="49,707,0,74" />
</Grid>


 

I have given name of UserControl as debugmodeSplashScreen

Opening UserControl in Popup

Below function will open user control in popup. You need to call below function inside the constructor of application start page.

image

Create Splash Screen Image from Popup

Make sure you are calling function OpenUserControlPopup in the constructor of the application start page and then while running in emulator

  • Take screen shot of popup in emulator
  • Paste screen shot in paint
  • By pressing Ctrl+W set width and height of the image as 480×800 Pixels.
  • Save image with the name SplashScreenImage.jpg
  • Right click on the project and add existing item. Select above saved image [from step 4] and add in the project.

Calling Service and Downloading data on background thread

You need to make call to service and download data in back ground thread. Once data is downloaded make popup visibility to false. Create a function to load data and instantiate a background thread in that.

image

In Do work you need to make a call to WCF service.

image

Till application is calling service and downloading data usercontrol in popup will be visible. Putting all the codes together, code behind for the application start page would be as below,

using System;
using Microsoft.Phone.Controls;
using System.Windows.Controls.Primitives;
using System.ComponentModel;
using System.Threading;
using PhoneApp1.ServiceReference1;

namespace PhoneApp1
{
public partial class MainPage : PhoneApplicationPage
{

private BackgroundWorker backroungWorker;
Popup popup;
public MainPage()
{
InitializeComponent();
OpenUserControlInPopup();


}
private void OpenUserControlInPopup()
{
this.popup = new Popup();
this.popup.Child = new debugmodeSplashScreen();
this.popup.IsOpen = true;
LoadData();

}

private void LoadData()
{
backroungWorker = new BackgroundWorker();
backroungWorker.DoWork +=
new DoWorkEventHandler(backroungWorker_DoWork);
backroungWorker.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(backroungWorker_RunWorkerCompleted);
backroungWorker.RunWorkerAsync();
}

void backroungWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.Dispatcher.BeginInvoke(() =>
{
this.popup.IsOpen = false;

}
);
}

void backroungWorker_DoWork(object sender, DoWorkEventArgs e)
{
// call service here .
Service1Client proxy = new Service1Client();
proxy.GetDataCompleted +=
new EventHandler<GetDataCompletedEventArgs>(proxy_GetDataCompleted);
proxy.GetDataAsync(9);
Thread.Sleep(8000);
}

void proxy_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
{
//Service result return
}
}
}


On running you should get splash screen with progress bar as below,

clip_image001

This is what all you need to do have a slash screen with progress bar in your application. I hope this post is useful. Thanks for reading.

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

How to work with WrapPanel in Windows Phone 7

In this post I will show you the various way of working with WrapPanel in Windows Phone 7 application. I will discuss

  1. Basic working of WrapPanel
  2. WrapPanel with ScrollBar
  3. Using WrapPanel in a ListBox

WrapPanel comes as part of Silverlight for Windows Phone Toolkit. You can download it from here . You need to install MSI package after downloading the toolkit to work with WrapPanel.

To start using toolkit add the namespace on xaml as below,


xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

Basic working of WrapPanel

I am going to put 16 red rectangles inside a WrapPanel. All rectangles would be in two columns of WrapPanel.


<toolkit:WrapPanel>
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Margin="10" Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" Margin="10" />
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" Margin="10" />
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" Margin="10" />
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" Margin="10" />
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" Margin="10" />
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" Margin="10" />
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" Margin="10" />
</toolkit:WrapPanel>

Default FlowDirection is Left to Right. If you want you can change it as below along with the basic properties like Height etc.

image

On running you will get output as below,

image

You will notice that there should be 8 rows visible since there are 16 rectangles but you are getting only 5 rows and not able to scroll as well.

WrapPanel with ScrollBar

To work with scroll in WrapPanel, you need to put WrapPanel inside a ScrollViewer as below,

image

You need to put WrapPanel inside ScrollViewer. I have done this and modified the code as below to vertically scroll through all the rows of rectangles.


<ScrollViewer VerticalScrollBarVisibility="Auto">
<toolkit:WrapPanel>
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Margin="10" Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" Margin="10" />
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" Margin="10" />
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" Margin="10" />
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" Margin="10" />
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" Margin="10" />
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" Margin="10" />
<Rectangle Height="100" Width="200" Fill="Red" />
<Rectangle Height="100" Width="200" Fill="Red" Margin="10" />
</toolkit:WrapPanel>
</ScrollViewer>

Now on running the application, you should able to scroll vertically all the rows.

Using WrapPanel in a ListBox

A very common requirement you may come across to work with WrapPanel and ListBox together. Imagine a scenario where you want

  • Button with Image as ListBox item
  • Wrapping of Button horizontally

Something like below,

image

What all you need to do is follow below steps,

Create a ListBox

image

Inside the ListBox you need to create ItemPanel and put a template inside it for the WrapPanel as below,

image

Then you need to create ItemTemplate and DataTemplate as below. Inside DataTemplate make sure you are putting StackPanel

image

Now you need to create Image Button. For that modify Button as below. I am Binding Text of TextBlock with Name Property of the class whereas for Image Source is fixed to Application.jpg

image

By putting all the codes together, you should have below code


<ListBox x:Name="lstData">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel>
<Button x:Name="btnData" >
<StackPanel Orientation="Vertical">
<Image Source="ApplicationIcon.jpg"
Width="100"
Height="100" />
<TextBlock Text="{Binding Name}" Width="100" />
</StackPanel>
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

In the code behind, I have created a class Student.


public class Student
{
public string Name { get; set; }
}

I have also created function to return many Students.


private List<Student> GetDummyData()
{
List<Student> lstStd = new List<Student>
{
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"},
new Student {Name ="a"}

};
return lstStd;
}

And then binding it to the ListBox as below,


public MainPage()
{
InitializeComponent();
lstData.ItemsSource = GetDummyData();
}


In this way you can work with WrapPanel and ListBox. I hope this post is useful. Thanks for reading.

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

How to navigate to other page from a user control in Windows Phone 7

Let us say, you have a scenario to navigate from user control

image

In code behind of the user control very first put below namespace,

clip_image001

Next globally define an event as below,

clip_image002

After this you need to create a method to handle page navigation event as below. This function is taking URI to navigate as input parameter.

clip_image003

Let us say you have a button on user control and you want to navigate on click event of that button then you need to call above function inside the click event of the button like below

clip_image005

Finally code behind of user control should look like below


using System;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace test
{
public partial class MyUserControl : UserControl
{

public event EventHandler EventForPageNavigation;

public MyUserControl()
{
InitializeComponent();
}

public void MethodToNavigateToPage(Uri uri)
{
var e = new NavigationEventArgs(null, uri);
if (EventForPageNavigation != null)
EventForPageNavigation(this, e);
}

private void btnNavigate_Click(object sender, System.Windows.RoutedEventArgs e)
{
MethodToNavigateToPage(new Uri("/ImageUpload.xaml", UriKind.Relative));
}
}
}

Next you need to handle this event of the main page where user control is being used. Let us say you are using user control with the name NavigationUserControl. What I mean while adding user control on the XAML of the main page you gave the name NavigationUserControl

First you will have registered the event in the OnNavigatedTo method or constructor of page as below,


NavigationUserControl.EventForPageNavigation += new EventHandler(NavigationUserControl _NavigateToPageEvent);

Then in navigated event navigate to URI as below,


void NavigationUserControl_NavigateToPageEvent(object sender, EventArgs e)
{
NavigationService.Navigate(((NavigationEventArgs)e).Uri);
}

So in this way you can perform navigation in a user control. I hope this post is useful. Thanks for reading

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.