Code to Share Status or Links on Social Media sites in Windows Phone

While creating application for Windows Phone you may come across requirement to share certain message from your application on social media sites configured on user device. You can do it using ShareStaus Task. ShareStausTask class is defined as below. It is inherited from ShareTaskBase class.

image

 

ShareStatus Task Launcher allows an application to launch a dialog that allows user to share status message on social network site.

A status message can be shared as below,


ShareStatusTask shareStatusTask = new ShareStatusTask();
shareStatusTask.Status = "Hey I am Shared By DebugMode";
shareStatusTask.Show();


You may come across other scenario where you may need to share HyperLink across Social Media sites from your application. You can do that using launcher ShareLinkTask.

ShareLinkTask class is defined as below,

image

ShareLink Task Launcher allows an application to launch a dialog that allows user to share links on social network site.

A link can be shared as below,



ShareLinkTask shareLinkTask = new ShareLinkTask();
shareLinkTask.Title = "DebugMode";
shareLinkTask.LinkUri = new Uri("http://debugmode.net", UriKind.Absolute);
shareLinkTask.Message = "Post of DebugMode.";
shareLinkTask.Show();

These launchers will be launched only if user has configured social media sites on their device. I hope this post is useful. Thanks for reading.

How to launch Call Task from Secondary Tile in Windows Phone 7

Recently I got a mail from one of the reader. She asked; How could be launch Call Task from Secondary Tile? In this post I am going to show the way to do that. Before you go ahead with this post, I strongly recommend reading below three posts for more on Live Tiles and Call Task.

Video on How to work with Live Tiles in Windows Phone 7

Live Tiles in Windows Phone 7.5 or Mango phone

Code to make call in Windows Phone 7

Delete Secondary Tiles in Windows Phone 7.5 or Mango Phone

Idea of launching Call Task from Secondary Tiles is very simple.

  1. On clicking of Secondary Tile user will get navigated to a blank page. Let us call that page as Page1.XAML
  2. On NavigatedTo method of the Page1.Xaml, we will instantiate Call Task and show call panel to user.

Let us create Secondary Tile on MainPage.Xaml. I have put a button on MainPage and on click event of the button Secondary Tile will get created.


private void btnCreateSecondaryTiles_Click(object sender, RoutedEventArgs e)
{

var newTile = new StandardTileData()
{
Title = "Blogs Update",
BackgroundImage = new Uri("background.png", UriKind.Relative),
Count = 42,
};
var uri = "/Page1.xaml?state=Live Tile";
ShellTile.Create(new Uri(uri, UriKind.Relative), newTile);



}

If you notice in above code, I have set navigation to Page1.Xaml. Now on onNavigatedTo() method we need to write code to launch Call Task.


protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
//base.OnNavigatedTo(e);
PhoneCallTask callTask = new PhoneCallTask();
callTask.PhoneNumber = "999999";
callTask.DisplayName = "debugMode";
callTask.Show();


}


 

On running you should be getting below output

image

I hope this post was useful. Thanks for reading.

How to consume WCF REST Service with JSON in Windows Phone 7

In this post I will show you the way to consume JSON WCF REST Service in Windows Phone 7. This post is divided into four parts.

  1. Creating simple WCF REST Service for ADD
  2. Consuming JSON REST Service in Windows Phone 7 application.
  3. Creating complex WCF REST Service with custom classes and types.
  4. Consuming complex JSON REST Service in Windows Phone 7 application.

Creating simple Add Service

To start with let us create a WCF REST Service returning JSON as below. There is one operation contact named Add in the service and that is taking two strings as input parameters and returning integer.


[ServiceContract]
public interface IService2
{

[OperationContract]
[WebGet(UriTemplate="/Add/{Number1}/{Number2}",RequestFormat=WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json
)]
int Add(string  Number1, string Number2);

}

Service is implemented as below,


using System;

namespace MultipleBindingWCF
{

public class Service1 : IService2
{

public int Add(string Number1, string Number2)
{
int num1 = Convert.ToInt32(Number1);
int num2 = Convert.ToInt32(Number2);
return num1 + num2;
}

}
}

Next in this section we need to configure service. We need to configure service webHttpBinding to eanble it as REST Service. So in Web.Config we need to do the below changes.


<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name ="servicebehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restbehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<endpoint name ="RESTEndPoint"
contract ="MultipleBindingWCF.IService2"
binding ="webHttpBinding"
address ="rest"
behaviorConfiguration ="restbehavior"/>
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>

After configuring, Service ready for hosting. You are free to host it either on Azure, IIS, or Cassini. For local ASP.Net Server hosting press F5. Do a testing of service in browser and if you are getting expected output, you are good to consume it in the Windows Phone 7 application.

Consuming Service in Windows Phone 7

To consume REST Service in Windows Phone 7 and then parse JSON, you need to add below references in Windows Phone 7 project.

 

 

 

As the design of the page I have put two textbox and one button. User will input numbers to be added in the textbox and on click event of button result would get displayed in message box. Essentially on click event of the button we will make a call to the service. Design of the page is as below,


<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="calling JSON REST" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="JSON REST" 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">
<TextBox x:Name="txtNumber1" Height="100" Margin="-6,158,6,349" />
<TextBox x:Name="txtNumber2" Height="100" Margin="-6,28,6,479" />
<Button x:Name="btnAdd" Height="100" Content="Add" Click="btnAdd_Click"/>
</Grid>
</Grid>

On click event of the button we need to make a call to the service as below,

clip_image002

And ServiceURi is constructed as below,

clip_image004

There is nothing much fancy about above service call. We are just downloading JSON as string using WebClient. However parsing of JSON is main focus of this post. To parse we need to write below code in completed event.

clip_image006

In above code

  • Converting downloaded string as Stream
  • Creating instance of DataContractJsonSerializer. In that we are passing type as string since returned type of service is string.
  • Reading stream into instance of DataContractJsonSerializer
  • Displaying the result.

Putting all code together you should have below code to make a call and parse JSON in windows phone 7


using System;
using System.Net;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;

namespace ConsumingJSON
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}

private void btnAdd_Click(object sender, RoutedEventArgs e)
{

string Number1 = txtNumber1.Text;
string Number2 = txtNumber2.Text;
string ServiceUri = "http://localhost:30576/Service1.svc/Rest/add/"
+ Number1 + "/"
+ Number2;
WebClient proxy = new WebClient();
proxy.DownloadStringCompleted +=
new DownloadStringCompletedEventHandler(proxy_DownloadStringCompleted);
proxy.DownloadStringAsync(new Uri(ServiceUri));

}

void proxy_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
Stream stream = new MemoryStream(Encoding.Unicode.GetBytes(e.Result));
DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(string));
string result = obj.ReadObject(stream).ToString();
MessageBox.Show(result);
}
}
}

Creating complex Service with custom classes and types

Go back to service and add a custom class as below,

[DataContract]
public class Student
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string RollNumber { get; set; }
[DataMember]
public string Grade { get; set; }
}

And add one more function to service. This operation contract will return List of Students.


[OperationContract]
[WebGet(UriTemplate = "/GetStudents", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json
)]
List<Student> GetStudents();

Next you need to implement service as below,


public List<Student> GetStudents()
{
List<Student> lstStudent = new List<Student>
{
new Student { Name = "John " ,RollNumber = "1" , Grade = "A"},
new Student { Name = "Dan " ,RollNumber = "2" , Grade = "Q"},
new Student { Name = "Pinal " ,RollNumber = "3" , Grade = "M"},
new Student { Name = "Mahesh " ,RollNumber = "4" , Grade = "Z"},
new Student { Name = "Julie" ,RollNumber = "5" , Grade = "L"},
};
return lstStudent;
}

Configuration will be the same as of simple REST Service returning JSON. Press F5 to host and test the service.

Consuming complex JSON REST Service in Windows Phone 7 application

At the Windows Phone 7 application, you need to create entity class. This class will be representing Student class of service. Add Student class to project as below,


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

}

Design page as below. I have put a button and on click event of the button service will get called and returned data would be bind to the listbox.


<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="110" />
</Grid.RowDefinitions>
<ListBox x:Name="lstStudents" Height="auto" Width="auto" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextTitle2Style}"  />
<TextBlock Text="{Binding RollNumber}" Style="{StaticResource PhoneTextTitle3Style}" />
<TextBlock Text="{Binding Grade}" Style="{StaticResource PhoneTextAccentStyle}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

<Button Grid.Row="1" x:Name="btnAdd" Height="100" Content="Add" Click="btnAdd_Click"/>
</Grid>

In the code behind on click event of the button we need to make call using WebClient again


private void btnAdd_Click(object sender, RoutedEventArgs e)
{
WebClient proxy = new WebClient();
proxy.DownloadStringCompleted += new DownloadStringCompletedEventHandler(proxy_DownloadStringCompleted);
proxy.DownloadStringAsync(new Uri("http://localhost:30576/Service1.svc/rest/getstudents"));

}

And need to parse the returned JSON as below. In parameter to create instance of DataContractJsonSrrializer we are passing List of Student.


void proxy_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
Stream stream = new MemoryStream(Encoding.Unicode.GetBytes(e.Result));
DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(List<Student>));
List<Student> result = obj.ReadObject(stream) as List<Student>;
lstStudents.ItemsSource = result;
}

On running you should be getting output as below.

image

In this way you can work with WCF REST with JSON Data and Windows Phone 7. I hope this post is is useful. Thanks for reading

Video on How to work with Live Tiles in Windows Phone 7

 

You can read more about Live Tiles in below  articles

Live Tiles in Windows Phone 7.5 or Mango phone

Delete Secondary Tiles in Windows Phone 7.5 or Mango Phone

Schedule the Tile update in Windows Phone 7.5 or Mango Phone

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.

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.

Video on How to work with System Tray Progress Indicator in Windows Phone 7

 

 

READ MORE HERE