Window in SilverLight 5 Via WCF Service


Introduction

Today, in this article let's concentrate on another SilverLight application, whereby communicating with a WCF Service to perform some operation.
 
Question: What is Window?
 
In simple terms SilverLight 5 applications enable us to create native windows which can run outside of the browser. 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_Window_SL

{

    // 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.ServiceModel.Web;

using System.Text;

 

namespace WCF_Window_SL

{

    // 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 ArthmeticWindow.xaml (UserControl) looks like this.

 

<UserControl x:Class="Silverlight_Window_Application.ArthmeticWindow"

    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"

    mc:Ignorable="d"

    d:DesignHeight="300" d:DesignWidth="400">

 

    <Grid x:Name="LayoutRoot" Background="White" Width="392">

 

        <Button Content="X"

                Height="23"

                HorizontalAlignment="Left"

                Background="Red"

                Margin="362,0,0,0"

                Name="btnClose"

                VerticalAlignment="Top"

                Width="26"

                Click="btnClose_Click"/>

 

        <Border BorderBrush="Silver"

                BorderThickness="3"

                Height="78"

                HorizontalAlignment="Left"

                Margin="12,109,0,0"

                Name="border1"

                VerticalAlignment="Top"

                Width="368">

 

            <TextBlock Foreground="DeepSkyBlue"

                       FontWeight="Bold" 

                       FontFamily="Gabriola"

                       Typography.StylisticSet7="True"

                       FontSize="25"

                       Width="276"

                       Height="52"

                       Name="txtResult"/>

        </Border>

 

        <TextBlock Height="23"

                   HorizontalAlignment="Left"

                   Margin="18,28,0,0"

                   Name="textBlock1"

                   Text="Please Enter First Number"

                   FontFamily="Verdana"

                   FontSize="15"

                   VerticalAlignment="Top" />

 

        <TextBlock Height="23"

                   HorizontalAlignment="Left"

                   Margin="18,81,0,0"

                   Name="textBlock2"

                   Text="Please Enter Second Number: "

                   FontFamily="Verdana"

                   FontSize="15"

                   VerticalAlignment="Top" />

 

        <TextBox Height="23"

                 HorizontalAlignment="Right"

                 Margin="0,28,4,0"

                 Name="textBox1"

                 VerticalAlignment="Top"

                 Width="120" />

 

        <TextBox Height="23"

                 HorizontalAlignment="Left"

                 Margin="268,80,0,0"

                 Name="textBox2"

                 VerticalAlignment="Top"

                 Width="120" />

 

        <Button Content="Addition"

                FontFamily="Verdana"

                FontSize="15"

                Background="DeepSkyBlue"

                Height="23"

                HorizontalAlignment="Left"

                Margin="12,207,0,0"

                Name="button1"

                VerticalAlignment="Top"

                Width="122"

                Click="button1_Click"/>

 

        <Button Background="DeepSkyBlue"

                Content="Subtraction"

                FontFamily="Verdana"

                FontSize="15"

                Height="23"

                HorizontalAlignment="Left"

                Margin="258,207,0,0"

                Name="button2"

                VerticalAlignment="Top"

                Width="122"

                Click="button2_Click"/>

 

        <Button Background="DeepSkyBlue"

                Content="Multiplication"

                FontFamily="Verdana"

                FontSize="15"

                Height="23"

                HorizontalAlignment="Left"

                Margin="58,265,0,0"

                Name="button3"

                VerticalAlignment="Top"

                Width="122"

                Click="button3_Click"/>

 

        <Button Background="DeepSkyBlue"

                Content="Division"

                FontFamily="Verdana"

                FontSize="15"

                Height="23"

                HorizontalAlignment="Left"

                Margin="221,265,0,0"

                Name="button4"

                VerticalAlignment="Top"

                Width="122"

                Click="button4_Click"/>

    </Grid>

</UserControl>

 

Step 6: The complete code of the ArthmeticWindow.xaml.cs (UserControl) 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 Silverlight_Window_Application.ServiceReference1;

 

namespace Silverlight_Window_Application

{

    public partial class ArthmeticWindow : UserControl

    {

        public ArthmeticWindow()

        {

            InitializeComponent();

            border1.Visibility = System.Windows.Visibility.Collapsed;

        }

        public void btnClose_Click(object sender, RoutedEventArgs e)

        {

            obj.Close();

        }

        private void add_Call(object sender, addCompletedEventArgs e)

        {

            txtResult.Text = "Addition Result is:  " + e.Result.ToString();

            border1.Visibility = System.Windows.Visibility.Visible;

        }

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            border1.Visibility = System.Windows.Visibility.Collapsed;

            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))

            {

                MessageBox.Show("Please Enter Some Values", "SilverLight 5 - Window via WCF", MessageBoxButton.OKCancel);

            }

            else

            {

                objClient.addCompleted += new EventHandler<addCompletedEventArgs>(add_Call);

                objClient.addAsync(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));

                textBox1.Text = "";

                textBox2.Text = "";

            }

        }

        private void sub_Call(object sender, subCompletedEventArgs e)

        {

            txtResult.Text = "Subtraction Result is:  " + e.Result.ToString();

            border1.Visibility = System.Windows.Visibility.Visible;

        }

        private void button2_Click(object sender, RoutedEventArgs e)

        {

            border1.Visibility = System.Windows.Visibility.Collapsed;

            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))

            {

                MessageBox.Show("Please Enter Some Values", "SilverLight 5 - Window via WCF", MessageBoxButton.OKCancel);

            }

            else

            {

                objClient.subCompleted += new EventHandler<subCompletedEventArgs>(sub_Call);

                objClient.subAsync(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));

                textBox1.Text = "";

                textBox2.Text = "";

            }

        }

 

        private void mul_Call(object sender, mulCompletedEventArgs e)

        {

            txtResult.Text = "Multiplication Result is:  " + e.Result.ToString();

            border1.Visibility = System.Windows.Visibility.Visible;

        }

 

        private void button3_Click(object sender, RoutedEventArgs e)

        {

            border1.Visibility = System.Windows.Visibility.Collapsed;

            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))

            {

                MessageBox.Show("Please Enter Some Values", "SilverLight 5 - Window via WCF", MessageBoxButton.OKCancel);

            }

            else

            {

                objClient.mulCompleted += new EventHandler<mulCompletedEventArgs>(mul_Call);

                objClient.mulAsync(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));

                textBox1.Text = "";

                textBox2.Text = "";

            }

        }

        private void div_Call(object sender, divCompletedEventArgs e)

        {

            txtResult.Text = "Division Result is:  " + e.Result.ToString();

            border1.Visibility = System.Windows.Visibility.Visible;

        }

        private void button4_Click(object sender, RoutedEventArgs e)

        {

            border1.Visibility = System.Windows.Visibility.Collapsed;

            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))

            {

                MessageBox.Show("Please Enter Some Values", "SilverLight 5 - Window via WCF", MessageBoxButton.OKCancel);

            }

            else

            {

                objClient.divCompleted += new EventHandler<divCompletedEventArgs>(div_Call);

                objClient.divAsync(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));

                textBox1.Text = "";

                textBox2.Text = "";

            }

        }

        #region Instance Varibles

        Service1Client objClient = new Service1Client();

        Window obj = new Window();

        #endregion

    }

}

 

Step 7: The complete code of the MainPage.xaml looks like this.

 

<UserControl x:Class="Silverlight_Window_Application.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"

    mc:Ignorable="d"

    d:DesignHeight="300" d:DesignWidth="389">

 

    <Grid x:Name="LayoutRoot" Background="White" Width="392">

 

        <Button Content="Click To Perform Arthmetic"

                FontFamily="Gabriola"

                FontWeight="Bold"

                FontSize="22"

                Height="67"

                HorizontalAlignment="Right"

                Margin="0,108,89,0"

                Name="button1"

                VerticalAlignment="Top"

                Width="206"

                Background="DeepSkyBlue"

                Click="button1_Click"

                Typography.StylisticSet7="True"/>

    </Grid>

</UserControl>

 

Step 8: 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;

 

namespace Silverlight_Window_Application

{

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            Window obj_Window = new Window();

            obj_Window.Width = 500;

            obj_Window.Height = 350;

            obj_Window.Title = "Arthimetic New Window SL 5 via WCF";

            obj_Window.Content = new ArthmeticWindow();

            obj_Window.Visibility = System.Windows.Visibility.Visible;

        }

 

    }

}

 

Step 9: Before you rebuild and run the application.


Window1.png 


Go To Properties of SilverLight Application.

 

Select the CheckBox, Enable running application out of browser.


img2.gif

 

Then, click on Out of Browser Setting - Button.

 

Select the CheckBox, Require Elevated trust when running the application outside of the browser.


Window3.png

 

Step 10: The output of the application looks like this.


Window4.png

 

Step 11: The output of the Arithmetic Window Application looks like this.


Window5.png

 

Step 12: The output of the Nothing Entered Application looks like this.


Window6.png

Step 13: The output of the Addition Operation Application looks like this.


Window7.png

Window8.png

I hope this article is useful for you. I look forward to your comments and feedback.

Up Next
    Ebook Download
    View all
    Learn
    View all
    MVC Corporation is consulting and IT services based company.