How to access LightSwitch methods from Silverlight Application: Part II


Prologue:

As we have discussed earlier in my article about accessing the LightSwitch methods from Silverlight application, in this article we shall discuss the same with different method of doing this.

So in this article part, I am going to explain the second way [Karol Zadora-Przylecki's method] to access the Methods of LightSwitch from Silverlight Application.

Difference between Sheel Shah and Karol Zadora-Przylecki's:

Functionality

Sheel Shah' method

Karol Zadora-Przylecki's method

Method type

Partial methods [native methods]

Public, private methods [user defined methods]

Return type

Void

Returns user defined type

Parameter

Null

N of parameters

Preparing LightSwitch Solution:

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

LSSil1.gif

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.

LSSil2.gif

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.

Defining Interface:

When we create a Screen, LightSwitch internally create a class for that Screen. We can't access the Methods or Properties of LightSwitch created class from outside Applications like Silverlight Application.

So if you want to access the properties and methods, we need to define an interface with properties and methods declared and need to implement that properties and methods in the LightSwitch created class.

To define an interface, switch the Solution Explorer view to File View.

LSSil3.gif

In the above figure,

  1. From Solution Explorer, Select the LightSwitch Application and Click on icon highlighted.
  2. It will show a dropdown list, select the "File View" from that list.

Now, you may see the some of the system generated projects. From this, some of the system generated projects are not visible. So make those projects visible by following the shown figure.

LSSil4.gif

Now select the "Common" project.

LSSil5.gif

Here,

  • Create a folder called "UserMethods".
  • Create an interface called "IUserMethods" as shown in the figure below which will be used to define the interface with methods and properties.

LSSil6.gif

After creating an interface, declare a method called "AskHowAreYou" as shown in the figure.

LSSil7.gif

Implementing Interface:

To access the methods, we need to implement the interface defined. Open the partial class file as shown in the below figure.

LSSil8.gif

As shown in the above figure, click on "Write Code" dropdown list to open the partial class file.

LSSil10.gif

Implement the "IUserMethods" interface as shown in the above figure.

LSSil11.gif

It is must to define the methods which is declared in the interface. If you place the cursor on the interface, it will show a small dropdown list. Click on that dropdown list; you will be getting an option to implement the methods as shown in the figure.

LSSil12.gif

As shown in the figure, just define the interface method called "AskHowAreYou" which will return the string.

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.

LSSil13.gif

Xaml for the Usercontrol:

LSSil14.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.

LSSil15.gif

These assemblies are in the below given path.

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

Now invoke the LightSwitch method as shown in the figure.

LSSil16.gif

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

Adding Silverlight Button Usercontrol to LightSwitch App:

LSSil17.gif

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

LSSil18.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:

LSSil19.gif

When you click on "Ask how are you" button, we are invoking the LightSwitch method with the parameter "Jagan". So it is displayed in the message box.

Summary:

In this article part II, we have seen about how to call Visual Studio LightSwitch 2011 application methods from Silverlight Application using Zadora-Przylecki's method.

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


Similar Articles