How to access LightSwitch methods from Silverlight Application: Part I


Prologue:

On of my colleague got a chance to work on LightSwitch Business Application. In that project, He supposed to use Silverlight Usercontrol to include more functionality and better UI. One of the functionality of Silverlight Usercontrol needs to access the Methods which are defined in LightSwitch.

He asked me that How to access the Property and Methods which are defined in LightSwitch from Silverlight Application. As he asked, I thought to explain Sheel Shah and Karol Zadora-Przylecki's way to access the methods and properties which is explained by Michael Washington.

So in this article, I am going to explain the first way [Sheel Shah Method] to access the Methods of LightSwitch from Silverlight Application as article part I.

Let us create a demo Visual Studio LightSwitch 2011 application to explain the situation discussed above.

Preparing LightSwitch Solution:

Start the Visual Studio LightSwitch 2011; create a LightSwitch project as shown in the below figure.

LightSwitch methods from Silverlight Application

In this demo application, we will simply define a method to say Hello to world when we click on Silverlight button control.

Designing Entity:

As we are simply saying hello to world, we don't need any entity.

Designing Screen:

Create a Search Screen as show in the figure given below.

LightSwitch methods from Silverlight Application

Here,

  1. Select "Search Data Screen" template from Screen template panel.
  2. As we have not designed any entity, simply select "None" from "Screen Data" dropdown list.

Adding LightSwitch Method:

Add the LightSwitch method as shown in the below figure.

LightSwitch methods from Silverlight Application

Here,

Open the screen designer then,

  1. Click on "Add Data Item..." button which is at the top of the screen designer.
  2. It will show a dialog box. In that, select "Method" option from the "Screen Member Type" group.
  3. Give the method name as "SayHello" and click on OK.

Editing the Method:

LgtSSht4.gif

Here,

  1. In the "Screen Member Panel" which is at lift side of the screen tree. From this select "SayHello" method and Right click it.
  2. From the Right click option, select "Edit Execute Code" which will create a method as like below one.

LgtSSht4.5.gif

It is important to build the LightSwitch application to generate the required dlls.

Creating Silverlight Button Usercontrol:

Create a Silverlight Application as shown in the figure.

LgtSSht5.gif

Xaml for the Usercontrol:

LgtSSht6.gif

Design the user control as like the above one.

Invoking the LightSwitch method:

To invoke the LightSwitch method, we need to add the LightSwitch project assembly as shown in the below figure.

LgtSSht7.gif

These assemblies are in the below given path.

  • ..\ClientGenerated\Bin\Debug\Microsoft.LightSwitch.dll
  • ..\ClientGenerated\Bin\Debug\Microsoft.LightSwitch.Client.dll

Now invoke the LightSwitch method as shown in the figure.

LgtSSht8.gif

It is important to build the Silverlight Application to generate the required dlls.

Adding Silverlight Button Usercontrol to LightSwitch App:

LgtSSht9.gif

Click on the "New Custom Control" option to add the Silverlight control.

LgtSSht10.gif

Here,

  1. Click on "Add Reference" button to point the Silverlight button control dlls.
  2. After pointing to Silverlight user control dlls, just click on "Main Page".
  3. Let it be the Data for the control is "Screen".

We have successfully added the Silverlight user control to LightSwitch application.

Hit F5 to see the Application in Action.

Application in Action:

LgtSSht11.gif

When you click on the Say Hello button, you will get a Message box saying "Hello".

Summary:

In this article part, we have seen about how to call Visual Studio LightSwitch 2011 application methods from Silverlight Application.

Thanks for spending your precious time here. Please provide your valuable feedbacks and comments, which make me to give a better article next time.
 

Next Recommended Readings