Introduction
In this article let's concentrate on another interesting SilverLight application, whereby communicating with a WCF Service to perform some operation.
Question : What is Single or N-ClickCount Functionality?
In simple terms "SilverLight 5 provides one of the top feature, which enables user to perform some operation based on number of mouse click count calculations. It may range from 1 to n clicks".
Let's get this implemented practically for a better idea of this!!!
Step 1: The complete code of the 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 Wcf_SL_Click
{
// 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 the 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 Wcf_SL_Click
{
// 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 the 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 the Clientaccesspolicy.xml looks like this (to avoid cross domain problem in SilverLight).
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="SOAPAction">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
Step 5: The complete code of the MainPage.xaml looks like this.
<UserControl x:Class="Rad_SL_Count.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800">
<Grid x:Name="LayoutRoot">
<TextBlock Height="23"
HorizontalAlignment="Left"
Margin="30,34,0,0"
Name="textBlock1"
Text="Please Enter First Number: "
FontFamily="Verdana"
FontSize="22"
VerticalAlignment="Top" />
<TextBlock FontFamily="Verdana"
FontSize="22" Height="23"
HorizontalAlignment="Left"
Margin="20,104,0,0"
Name="textBlock2"
Text="Please Enter Second Number: "
VerticalAlignment="Top" />
<TextBox Height="43"
HorizontalAlignment="Left"
Margin="431,25,0,0"
Name="textBox1"
VerticalAlignment="Top"
Width="150" />
<TextBox Height="43"
HorizontalAlignment="Left"
Margin="431,95,0,0"
Name="textBox2"
VerticalAlignment="Top"
Width="150" />
<Border BorderBrush="Silver"
BorderThickness="3"
Background="DarkGray"
Height="100"
Name="border1"
Width="200"
Margin="42,169,398,211"
MouseLeftButtonDown="button1_MouseLeftButtonDown">
<TextBlock Text="Click To Arthmetic"
FontFamily="Verdana"
FontSize="17"
Foreground="LightCyan"
FontWeight="Bold"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Width="178"
Height="22" />
</Border>
</Grid>
</UserControl>
Step 6: The complete code of the 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 Rad_SL_Count.ServiceReference1;
namespace Rad_SL_Count
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void add_Call(object sender, addCompletedEventArgs e)
{
MessageBox.Show("Addition Result is: " + e.Result.ToString(), "Click Count -SL5 via WCF Service", MessageBoxButton.OKCancel);
}
private void sub_Call(object sender, subCompletedEventArgs e)
{
MessageBox.Show("Subtraction Result is: " + e.Result.ToString(), "Click Count -SL5 via WCF Service", MessageBoxButton.OKCancel);
}
private void mul_Call(object sender, mulCompletedEventArgs e)
{
MessageBox.Show("Multiplication Result is: " + e.Result.ToString(), "Click Count -SL5 via WCF Service", MessageBoxButton.OKCancel);
}
private void div_Call(object sender, divCompletedEventArgs e)
{
MessageBox.Show("Division Result is: " + e.Result.ToString(), "Click Count -SL5 via WCF Service", MessageBoxButton.OKCancel);
}
private void button1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("Please Enter Some Values", "Click Count - SL 5 via WCF", MessageBoxButton.OKCancel);
}
else
{
if (e.ClickCount == 1)
{
objClient.addCompleted += new EventHandler<addCompletedEventArgs>(add_Call);
objClient.addAsync(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));
}
else if (e.ClickCount == 2)
{
objClient.subCompleted += new EventHandler<subCompletedEventArgs>(sub_Call);
objClient.subAsync(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));
}
else if (e.ClickCount == 3)
{
objClient.mulCompleted += new EventHandler<mulCompletedEventArgs>(mul_Call);
objClient.mulAsync(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));
}
else if (e.ClickCount == 4)
{
objClient.divCompleted += new EventHandler<divCompletedEventArgs>(div_Call);
objClient.divAsync(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));
}
else if (e.ClickCount > 4)
{
MessageBox.Show("Click Count Exceed--Nothing Performed", "Click Count - SL5 via WCF", MessageBoxButton.OKCancel);
}
textBox1.Text = "";
textBox2.Text = "";
}
}
#region Instance Variables
Service1Client objClient = new Service1Client();
#endregion
}
}
Step 7: The output of the application looks like this.
Step 8: The output of the Nothing Entered Application looks like this.
Step 9: The output of the Addition Operation Application looks like this.
Step 10: The output of the Subtraction Operation Application looks like this.
I hope this article is useful for you ...I look forward to your comments and feedback....Thanks.