Sunday, September 4, 2011

Silverlight Introduction

My previous post explained about COM objects access in Silverlight application, since many doesn't know Silverlight, i will give small introduction to silverlight in this post.

Silverlight is a Browser-plugin which provides rich internet applications !, Its a client-side web technology, If you know Applets in java, this is something very similar to it but its based on Dot Net.

Silverlight is embedded in HTML, It can access client computer's resources (which i have explained as accessing COM objects in previous post), It provides rich user-interface. It supports media and graphics.

To make your web application more interactive, attractive you need silverlight, its works on any browser and any OS.

You must have browsed many web-sites and seen a box still loading with java symbol, thats Applet, similar to that Silverlight also loads parallel to web page. Silverlight runtime should be installed on client-system just like Java-runtime.

Silverlight uses XAML (Extensible Application Mark-up Language) for designing user-interface. Its simple and very easy to learn, It looks like XML + HTML. Operations to be performed in result of the interaction by user is programmed in C#.
eg: A button is placed and designed using XAML. What operations have to be performed for the click of that button is programmed using C#.

Visual studio can be used for developing a silverlight application. Choose New project-> silverlight application.
Interface should be designed in .xaml file, you can use drag-drop option of visual studio IDE and easily design interface without typing any XAML code or you can type code yourself.
Event handlers for these interface elements should be in .cs file i.e. C# code.

When you execute, it will dynamically create an HTML page and embed this silverlight application in it, but if you want to have your own HTML file, you can embed it this way.

<object height="90%"
type="application/x-silverlight-2"
data="data:application/x-silverlight-2,"
style="width: 85%; position:relative;left:5%;top:5%" >
<param name="source" value="Bin/Debug/Weighing machine.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="5.0.60401.0" />
<param name="autoUpgrade" value="true" />
a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.60310.0"
style="text-decoration: none;">
<img
src="http://go.microsoft.com/fwlink/?LinkId=161376"
alt="Get Microsoft Silverlight"
style="border-style: none"/>
</a>
<object>


anchor tag here comes as an alternate if your application is not loaded onto web page for some problem just like alternate text for an image tag.

Any doubts, can ask in comments.
Will try my level best to clarify.




Saturday, September 3, 2011

Silverlight 5 : In browser application for accessing COM objects

I have been working on Silverlight for some time now and particulary silverlight 5, elevated trust in "In-browser" applications. So trying to spread whatever knowledge I have on it

There are 2 ways of achieving COM objects access in your silverlight application
1. If you host your silverlight application on a website, you will get that access automatically.
you should select "host on a website" option while creating silverlight application in Visual studio.

2. If you are not planning to host it on a website and just want an HTML file or just silverlight application running in browser, following are the steps

  • Create a silverlight 5 project without selecting host on a website option
  • Type "regedit" in windows->run , registry editor opens, navigate to HKEY_LOCAL_MACHINE->Software->Microsoft->silverlight Double click on silverlight. You should create a new registry variable now right click and select new->DWORD(32-bit value) . Give name as "AllowElevatedTrustAppsInBrowser" and set value as 1
  • In project->properties, navigate to silverlight tab, check "Enable running application out of browser". Click on out of browser settings, in that check "Require elevated trust when running outside the browser".
  • Project->properties, navigate to Debug tab and select Dynamically create a web page or specific page and choose your HTML file which embeds silverlight application.
  • Project->properties, navigate to Signing, check Sign the XAP file, click on Create test certificate, you will be asked to enter password. After that, click on more details, install the certificate in Trusted publishers and Trusted root certification authorities stores.
Now you can design your interface with XAML and give functionality with C#, Code to access COM objects are as follows.

A reference to Microsoft.Csharp should be added.

using System.Runtime.InteropServices.Automation;

dynamic com=com = AutomationFactory.CreateObject("Word.Application");

word.Visible = true;

dynamic doc = word.Documents.Add();

string insertText = "Hello World from Silverlight 5!";
dynamic range = doc.Range(0, 0);
range.Text = insertText;

This code opens Microsoft word, creates new document and inserts "hello world from silverlight 5!" text.

Now when you run this application, it runs in-browser and performs the operation.

After developing the application, you would like to deploy it in some client system, how do you do it.

1. same step as explained above for "regedit", add that registry value.
2. You should also install certificate, but while developing Visual studio has done certificate creation and signing XAP file just by click of buttons, you have to do this step manually for deploying it to other system.
Steps to create certificate and signing XAP file.

Run Visual studio command line tools as administrator and type the following commands.
  • makecert -r -pe -n "CN=sampleapplication" -ss bharath -sr CurrentUser -a sha1 -sky signature -sv sample.pvk sample.cer
  • pvk2pfx -pvk sample.pvk -spc sample.cer -pfx sample.pfx
  • signtool sign /v /f sample.pfx C:\Users\bharath\Desktop\sample.xap
you will find sample.cer and sample.pfx certificate files C:\windows\system32 folder.
copy those 2 files along with XAP + HTML file to the client system where you would like to run it.

double click on sample.cer file to install it in Trusted root certification authorities store
and double click on sample.pfx file to install it in Trusted Publishers store.

now you can open HTML file in-browser and your application will run fine.