Today, in this article
let' dig out with one more wonderful concept in windows phone whereby
communicating with WCF Service to perform some expected arithmetic operation.
Once Implemented, it gives flexibility to the user to access phone pivot
features and perform expected operation in much easy manner.
Question Arises:
What is Pivot Control?
In Simple Terms "It is a
simple light-weighted control which enables phone user to manage or navigate
across different pages very easily. It enables the tasks to be turned on easily.
It can be viewed from left to right or right to left depends upon screen touch
scrolling by the user."
So, let's get it started
off!!!
Step 1: The Complete Code
of IService1.cs looks like this:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.ServiceModel.Web;
using
System.Text;
namespace
Pivot_WCF
{
// NOTE: You can use the "Rename" command on the "Refactor"
menu to change the interface name "IService1" in both code and config file
together.
[ServiceContract]
public interface
IService1
{
[OperationContract]
double Add(double
a, double b);
[OperationContract]
double Sub(double
a, double b);
[OperationContract]
double Mul(double
a, double b);
[OperationContract]
double Div(double
a, double b);
}
}
Step 2: The Complete Code
of Service1.svc.cs looks like this:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.ServiceModel.Web;
using
System.Text;
namespace
Pivot_WCF
{
// NOTE: You can use the "Rename" command on the "Refactor"
menu to change the class name "Service1" in code, svc and config file together.
public class
Service1 : IService1
{
public
double Add(double
a, double b)
{
return a + b;
}
public
double Sub(double
a, double b)
{
return a - b;
}
public
double Mul(double
a, double b)
{
return a * b;
}
public
double Div(double
a, double b)
{
return a / b;
}
}
}
Step 3: The Complete Code
of Web.Config looks like this:
<?xml
version="1.0"?>
<configuration>
<system.web>
<compilation
debug="true"
targetFramework="4.0"
/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid
disclosing metadata information, set the value below to false and remove the
metadata endpoint above before deployment
-->
<serviceMetadata
httpGetEnabled="true"/>
<!--
To receive exception details in faults for debugging purposes, set the value
below to true. Set to false before deployment to avoid disclosing exception
information -->
<serviceDebug
includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment
multipleSiteBindingsEnabled="true"
/>
</system.serviceModel>
<system.webServer>
<modules
runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Step 4: The Complete Code
of MainPage.xaml looks like this:
<phone:PhoneApplicationPage
x:Class="Pivot_Control_Application.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignWidth="480"
d:DesignHeight="768"
d:DataContext="{d:DesignData
SampleData/MainViewModelSampleData.xaml}"
FontFamily="{StaticResource
PhoneFontFamilyNormal}"
FontSize="{StaticResource
PhoneFontSizeNormal}"
Foreground="{StaticResource
PhoneForegroundBrush}"
SupportedOrientations="Portrait"
Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot
is the root grid where all page content is placed-->
<Grid
x:Name="LayoutRoot"
Background="Transparent">
<!--Pivot Control-->
<controls:Pivot
Title="Vijay's Pivot
Application via WCF Service" FontFamily="Verdana"
FontSize="22">
<!--Pivot item one-->
<controls:PivotItem
Header="Addition">
<!--Double line list with
text wrapping-->
<StackPanel
x:Name="stackpanel1"
Width="500"
FlowDirection="LeftToRight"
>
<TextBlock x:Name="textBlock1"
FontFamily="Verdana"
FontSize="22"
Text="Please Enter First
Number" Height="60"
Width="293"
/>
<TextBox
Width="266"
x:Name="textbox1"></TextBox>
<TextBlock
x:Name="textBlock2"
Text="Please Enter
Second Number" FontFamily="Verdana"
FontSize="22"
Height="57"
Width="322"
/>
<TextBox
x:Name="textbox2"
Width="254"
/>
<Button
Content="Addition"
FontFamily="Verdana"
FontSize="22"
x:Name="btnAdd1"
Width="249"
Click="btnAdd1_Click"/>
<TextBlock
x:Name="textBlock3"
FontFamily="Verdana"
Foreground="Red"
FontSize="22"
Width="352"
Height="54"
/>
</StackPanel>
</controls:PivotItem>
<!--Pivot item two-->
<controls:PivotItem
Header="Substract">
<!--Triple line list no text
wrapping-->
<StackPanel
x:Name="stackpanel2"
Width="500"
FlowDirection="LeftToRight"
>
<TextBlock x:Name="textBlock4"
FontFamily="Verdana"
FontSize="22"
Text="Please Enter First
Number" Height="60"
Width="293"
/>
<TextBox
Width="266"
x:Name="textbox3"></TextBox>
<TextBlock
x:Name="textBlock5"
Text="Please Enter
Second Number" FontFamily="Verdana"
FontSize="22"
Height="57"
Width="322"
/>
<TextBox
x:Name="textbox4"
Width="254"
/>
<Button
Content="Substraction"
FontFamily="Verdana"
FontSize="22"
x:Name="btnSub1"
Width="249"
Click="btnSub1_Click"/>
<TextBlock
x:Name="textBlock6"
FontFamily="Verdana"
Foreground="Red"
FontSize="22"
Width="352"
Height="54"
/>
</StackPanel>
</controls:PivotItem>
<controls:PivotItem
Header="Multiply">
<!--Triple line list no
text wrapping-->
<StackPanel
x:Name="stackpanel3"
Width="500"
FlowDirection="LeftToRight"
>
<TextBlock x:Name="textBlock7"
FontFamily="Verdana"
FontSize="22"
Text="Please Enter First
Number" Height="60"
Width="293"
/>
<TextBox
Width="266"
x:Name="textbox5"></TextBox>
<TextBlock
x:Name="textBlock8"
Text="Please Enter
Second Number" FontFamily="Verdana"
FontSize="22"
Height="57"
Width="322"
/>
<TextBox
x:Name="textbox6"
Width="254"
/>
<Button
Content="Multiplication"
FontFamily="Verdana"
FontSize="22"
x:Name="btnMul1"
Width="249"
Click="btnMul1_Click"/>
<TextBlock
x:Name="textBlock9"
FontFamily="Verdana"
Foreground="Red"
FontSize="22"
Width="352"
Height="54"
/>
</StackPanel>
</controls:PivotItem>
<controls:PivotItem
Header="Divide">
<StackPanel
x:Name="stackpanel4"
Width="500"
FlowDirection="LeftToRight"
>
<TextBlock x:Name="textBlock10"
FontFamily="Verdana"
FontSize="22"
Text="Please Enter First
Number" Height="60"
Width="293"
/>
<TextBox
Width="266"
x:Name="textbox7"></TextBox>
<TextBlock
x:Name="textBlock11"
Text="Please Enter
Second Number" FontFamily="Verdana"
FontSize="22"
Height="57"
Width="322"
/>
<TextBox
x:Name="textbox8"
Width="254"
/>
<Button
Content="Division"
FontFamily="Verdana"
FontSize="22"
x:Name="btnDiv1"
Width="249"
Click="btnDiv1_Click"
/>
<TextBlock
x:Name="textBlock12"
FontFamily="Verdana"
Foreground="Red"
FontSize="22"
Width="352"
Height="54"
/>
</StackPanel>
</controls:PivotItem>
</controls:Pivot>
</Grid>
<!--Sample
code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar
IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png"
Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png"
Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
</phone:PhoneApplicationPage>
Step 5: The Complete Code of MainPage.xaml.cs looks like this:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
Microsoft.Phone.Controls;
using
Pivot_Control_Application.ServiceReference1;
namespace
Pivot_Control_Application
{
public partial
class MainPage
: PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
// Load
data for the ViewModel Items
private
void MainPage_Loaded(object sender,
RoutedEventArgs e)
{
}
static
void add_Call(object
sender, AddCompletedEventArgs e)
{
MessageBox.Show(" Addition Result is: "
+ e.Result.ToString());
}
private
void btnAdd1_Click(object
sender, RoutedEventArgs e)
{
textBlock3.Text = "";
if (string.IsNullOrEmpty(textbox1.Text)
|| string.IsNullOrEmpty(textbox2.Text))
{
textBlock3.Text = "Please Enter Some
Values";
}
else
{
Service1Client objClient = new
Service1Client();
objClient.AddCompleted += new
EventHandler<AddCompletedEventArgs>(add_Call);
objClient.AddAsync(Convert.ToDouble(textbox1.Text),
Convert.ToDouble(textbox2.Text));
textbox1.Text = "";
textbox2.Text = "";
}
}
static
void sub_Call(object
sender, SubCompletedEventArgs e)
{
MessageBox.Show(" Substraction Result
is: " + e.Result.ToString());
}
private void
btnSub1_Click(object sender, RoutedEventArgs e)
{
textBlock6.Text = "";
if (string.IsNullOrEmpty(textbox3.Text)
|| string.IsNullOrEmpty(textbox4.Text))
{
textBlock6.Text = "Please Enter Some
Values";
}
else
{
Service1Client objClient = new
Service1Client();
objClient.SubCompleted += new
EventHandler<SubCompletedEventArgs>(sub_Call);
objClient.SubAsync(Convert.ToDouble(textbox3.Text),
Convert.ToDouble(textbox4.Text));
textbox3.Text = "";
textbox4.Text = "";
}
}
static
void mul_Call(object
sender, MulCompletedEventArgs e)
{
MessageBox.Show(" Multiplication Result
is: " + e.Result.ToString());
}
private
void btnMul1_Click(object
sender, RoutedEventArgs e)
{
textBlock9.Text = "";
if (string.IsNullOrEmpty(textbox5.Text)
|| string.IsNullOrEmpty(textbox6.Text))
{
textBlock9.Text = "Please Enter Some
Values";
}
else
{
Service1Client objClient = new
Service1Client();
objClient.MulCompleted += new
EventHandler<MulCompletedEventArgs>(mul_Call);
objClient.MulAsync(Convert.ToDouble(textbox5.Text),
Convert.ToDouble(textbox6.Text));
textbox5.Text = "";
textbox6.Text = "";
}
}
static
void div_Call(object
sender, DivCompletedEventArgs e)
{
MessageBox.Show(" Division Result is: "
+ e.Result.ToString());
}
private
void btnDiv1_Click(object
sender, RoutedEventArgs e)
{
textBlock12.Text = "";
if (string.IsNullOrEmpty(textbox7.Text)
|| string.IsNullOrEmpty(textbox8.Text))
{
textBlock12.Text = "Please Enter
Some Values";
}
else
{
Service1Client objClient = new
Service1Client();
objClient.DivCompleted += new
EventHandler<DivCompletedEventArgs>(div_Call);
objClient.DivAsync(Convert.ToDouble(textbox7.Text),
Convert.ToDouble(textbox8.Text));
textbox7.Text = "";
textbox8.Text = "";
}
}
}
}
Step 6: The Output of
the Application looks like this:
Step 7: The Addition Operation Output Application looks like this:
Step 8: The Subtraction Operation Output Application looks like this:
Step 9: The Nothing Entered Output Application looks like this:
I hope this article is
useful for you.