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.
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.
Here,
- Select "Search Data Screen" template from
Screen template panel.
- 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.
In the above figure,
- From Solution Explorer, Select the
LightSwitch Application and Click on icon highlighted.
- 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.
Now select the "Common" project.
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.
After creating an interface, declare a method called "AskHowAreYou" as shown in
the figure.
Implementing Interface:
To access the methods, we need to implement the interface defined. Open the
partial class file as shown in the below figure.
As shown in the above figure, click on "Write Code" dropdown list to open the
partial class file.
Implement the "IUserMethods" interface as shown in the above figure.
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.
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.
Xaml for the Usercontrol:
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.
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.
It is important to build the Silverlight Application to generate the required
dlls.
Adding Silverlight Button Usercontrol to LightSwitch App:
Click on the "New Custom Control" option to add the Silverlight control.
Here,
- Click on "Add Reference" button to point
the Silverlight button control dlls.
- After pointing to Silverlight user control
dlls, just click on "Main Page".
- 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:
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.