Silverlight 3 - Experiencing ‘Out Of Browser’ Silverlight Application and Connecting it to a WebService

Posted by: Harsh Bardhan , on 4/3/2009, in Category Silverlight 2, 3, 4 and 5
Views: 29283
Abstract: Silverlight 3 came out with many new features. Out of these new features, one feature is the “Out Of Browser Support” for Silverlight applications. What it means, is that now, we will be able to run our Silverlight applications from our desktop. When I saw that the first time, my reaction was a WOW. But then is this Out Of Browser app capable of utilizing services that my application connects to? In this sample, we will create an ‘Out of Browser’ Silverlight application and will connect that application to a web service.
Silverlight 3 - Experiencing ‘Out Of Browser’ Silverlight Application and Connecting it to a WebService
 
Silverlight 3 came out with many new features. Out of these new features, one feature is the “Out Of Browser Support” for Silverlight applications. What it means, is that now, we will be able to run our Silverlight applications from our desktop. When I saw that the first time, my reaction was a WOW. But then is this Out Of Browser app capable of utilizing services that my application connects to? In this sample, we will create an ‘Out of Browser’ Silverlight application and will connect that application to a web service.
I am assuming that you all have Silverlight 3 installed on your machine. If not, read my article for setting up the Silverlight 3 development environment.
When you run any Silverlight 3 application in the browser and do a right click on that application, you will notice that you now get a new option for installing that application on your computers (see screenshot below)
In this screen shot, initially the “Install onto this computer” option is disabled. For enabling this, we first need to change a few settings. During the first half of this tutorial, we will see how to enable that.
Right Click
Step 1: Open Visual Studio 2008 > File > New Project > Select the language (C# or VB.NET) > Select ‘Silverlight’ in the Project Types > from the templates, select ‘Silverlight Application’.
New Project
Step 2: Type a name (OutOfBrowserApp) and location for the project and click OK (see Figure below). Choose the first option ‘Add a new Web to the solution for hosting the control’ and the project type as ‘Web Site’ and click OK. You will see that two projects are created: OutOfBrowserApp.Web and OutOfBrowserApp.
New Silverlight App
Step 3: Open MainPage.xaml file and add a Button to it. Your page will look like this after adding the button.
 <UserControl x:Class="OutOfBrowserApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button Click="Button_Click"></Button>
    </Grid>
</UserControl>
Step 4:Now write some code in the Click Handler of the Button. I have added a simple MessageBox code. Your code behind file will look like this.
C#
namespace OutOfBrowserApp
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Hi");
        }
    }
}
VB.NET
Namespace OutOfBrowserApp
      Partial Public Class MainPage
            Inherits UserControl
            Public Sub New()
                  InitializeComponent()
            End Sub
 
            Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
                  MessageBox.Show("Hi")
            End Sub
      End Class
End Namespace
Step 5: Time to change a few  configuration settings now!
Open the file  “AppManifest.xml “ from the Properties folder in the Silverlight application. You will notice there are a few lines commented there (see screen shot below). For creating Silverlight out of browser applications, all we need to do is to uncomment these lines!
App Manifest
We will also change the ‘Title’ of our application and other parameters accordingly. So our AppManifest.xml file after the changes will look like this.
<Deploymentxmlns="http://schemas.microsoft.com/client/2007/deployment"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    <Deployment.Parts>
    </Deployment.Parts>
 
  
    <Deployment.ApplicationIdentity>
        <ApplicationIdentity
            ShortName="Out of Browser Silverlight Application"
            Title="First Out Of Browser Application">
            <ApplicationIdentity.Blurb>This is a Out of Browser Silverlight application</ApplicationIdentity.Blurb>
        </ApplicationIdentity>
    </Deployment.ApplicationIdentity>
  
</Deployment>
 
Step 6: And we are all set. Run this application. When you now right click on your Silverlight application, you will get an option to install this application and that option is ‘Enabled’ this time (see screen shot below)
OutOfBrowserApp
Step 7: Click the ‘Install Out of browser Silverlight’ option and you will get an option to select where you want to install that application, as shown below in the screen shot
Install App
Step 8: Let’s select ‘StartMenu’ and click on OK. Now when you go to the Start menu (Start > All Programs), you will see the “Out of Silverlight Browser Application”.
Click on that and you will see that your Silverlight application opens up in a window.
That was easy right!
Now I was thinking if the same Silverlight application is consuming any services, then what happens? So out of curiosity, I just thought of creating a WebService and then consuming that in the Silverlight Application. We will then save this Silverlight application to our desktop and check whether it is able to consume these services in a similar fashion or not.
We will use the same application.
Step 9: Let us add a service and then refer that service in our Silverlight application.
Go to the “OutOfBrowserApp.Web” application and add a webservice there. Name it as ‘OutOfBrowserService.asmx’. It will create a hello world method which returns a string. Let’s not change anything here.
Add Web Service  
Step 10: Now go back to your Silverlight Application and add a reference to this Service. As the service is in the same solution, clicking on discover will automatically locate that service. Specify a namespace for that service reference (I have specified ‘OutOfBrowserService’)
Add Service Reference
Step 11: Modify the code in theMainPage.xaml.cs or MainPage.xaml.vb to make a call to this service. Our code will look like this.
C#
namespace OutOfBrowserApp
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
           OutOfBrowserService.OutOfBrowserServiceSoapClient outOfBrowserClient=new OutOfBrowserApp.OutOfBrowserService.OutOfBrowserServiceSoapClient();
           outOfBrowserClient.HelloWorldCompleted += new EventHandler<OutOfBrowserApp.OutOfBrowserService.HelloWorldCompletedEventArgs>(outOfBrowserClient_HelloWorldCompleted);
            outOfBrowserClient.HelloWorldAsync();       
        }
 
        void outOfBrowserClient_HelloWorldCompleted(object sender, OutOfBrowserApp.OutOfBrowserService.HelloWorldCompletedEventArgs e)
        {
         MessageBox.Show(e.Result);
         }
    }
}
VB.NET
Namespace OutOfBrowserApp
      Partial Public Class MainPage
            Inherits UserControl
            Public Sub New()
                  InitializeComponent()
            End Sub
 
            Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
               Dim outOfBrowserClient As OutOfBrowserService.OutOfBrowserServiceSoapClient = New OutOfBrowserApp.OutOfBrowserService.OutOfBrowserServiceSoapClient()
               AddHandler outOfBrowserClient.HelloWorldCompleted, AddressOf outOfBrowserClient_HelloWorldCompleted
                  outOfBrowserClient.HelloWorldAsync()
            End Sub
 
            Private Sub outOfBrowserClient_HelloWorldCompleted(ByVal sender As Object, ByVal e As OutOfBrowserApp.OutOfBrowserService.HelloWorldCompletedEventArgs)
             MessageBox.Show(e.Result)
            End Sub
      End Class
End Namespace
Step 12: Run your Application and save it to desktop/Start menu as we did previously.
You will observe, as long as your service is running and your application is run from your start menu, you will get the desired result which also tells us that our Out Of Browser application IS capable of getting data from services. See screen shot below.
Application Running
WOW again!
 
If you liked the article,  Subscribe to the RSS Feed or Subscribe Via Email  
Harsh Bardhan (MCTS) has developed IT solutions with a diverse background in server side and client side development. He is currently working with SymphonyServices as a Software developer. Harsh continues to be an integral part of open forums on cutting-edge technology like Silverlight, including the .NET Framework and Web Services. Harsh has expertise with many Microsoft technologies, including .NET, LINQ, WCF Service, Silverlight and a strong background in SQLServer. 

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on .NET Standard and the upcoming C# 8.0 too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Author bio not available


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Shoban Kumar on Friday, April 3, 2009 4:21 AM
Thanks for the tutorial. This is a great feature. Now it Adobe AIR gets a competitor. ;-)
Comment posted by Shoban Kumar on Friday, April 3, 2009 6:02 AM
Thanks for the tutorial. This is a great feature. Now it Adobe AIR gets a competitor. ;-)
Comment posted by Suprotim Agarwal on Saturday, April 4, 2009 11:33 AM
I wouldn't say that :)

http://blog.digitalbackcountry.com/2009/03/differences-between-silverlight-out-of-browser-experience-and-air/
Comment posted by Suprotim Agarwal on Saturday, April 4, 2009 11:34 AM
I wouldn't say that :)

http://blog.digitalbackcountry.com/2009/03/differences-between-silverlight-out-of-browser-experience-and-air/
Comment posted by avirag on Thursday, July 2, 2009 4:16 AM
this is a vry good feature.....can u tell me how can we use flash videos in silverlight and also to play in a video player......thanks in advance 4 this.....

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

C# .NET BOOK

C# Book for Building Concepts and Interviews

Tags

JQUERY COOKBOOK

jQuery CookBook