|
|
 |  |  |
 |
|
 |
 |
|
|
|
|
| Author |
Messages |
|
elizas
Posts:1
 |
| 02/02/2010 2:49 AM |
|
One of the aim of Silverlight developer is to keep the size application package i.e .xap file as small as possible.The larger the XAP file, the longer it takes to download, and if it grows too large, Silverlight might be unable to load it. Below are the steps to load assembly on demand and with the benefits of strong typing. Step 1. Create a Silverlight Class library. For example say "SampleLibrary" Step 2. Add a class to the SampleLibrary Public Class SampleClass Public Function SampleFunction() As String Return "From External library" End Function End Class Step 3. Create a Silverlight web application Step 4. Add Reference to SampleLibrary. After adding reference right-click on the SampleLibrary reference and change the property "Copy Local" to false. This will keeping the Assembly out of the XAP. Step 5. Copy the assembly to ClientBin folder of web application where the .xap file resides. Step 6. Add following to to load Assembly. WebClient : Provides common methods for sending data to and receiving data from a resource identified by a URI. Dim wc As WebClient = New WebClient() AddHandler wc.OpenReadCompleted, AddressOf wc_OpenReadCompleted wc.OpenReadAsync(New Uri("SampleLibrary.dll", UriKind.Relative)) After loading assembly from web server, AssemblyPart class's Load method loads it into the appdomain. Public Sub wc_OpenReadCompleted(ByVal sender As Object, ByVal e As System.Net.OpenReadCompletedEventArgs) Dim part As AssemblyPart = New AssemblyPart If Not e.Result Is Nothing Then part.Load(e.Result) UseClassLibrary() End If End Sub Step 7. To use Strong Typing: Do not create the on-demand assempbly class object inside wc_OpenReadCompleted as silverlight's JIT complier will come in the way. When JIT compiler compiles wc_OpenReadCompleted method, it scans the method, finds that it references a type named "SampleClass" so tries to load "SampleLibrary.dll" so that reference can be resolved. Unfortunately, this happens before the method is even executed. So always use the on-demand assembly classes in another method. In this way you can enjoy the benifits of strong typing Public Sub UseClassLibrary() Dim objSample As SampleLibrary.SampleClass = New SampleLibrary.SampleClass Show.Text = objSample.SampleFunction() End Sub I hope it was informative and ot did add some value. Thanks Eliza |
|
Cheers, Eliza |
|
|
Michael Washington
Posts:64
 |
| 02/02/2010 5:15 AM |
|
Thank you for sharing. Keep in mind that SilverlightDesktop is loading modules that it does not know about beforehand so it cannot add a reference. It always loads fast because it doesn't load any modules until they are requested. |
|
|
|
|
|
| You are not authorized to post a reply. |
|
|
|
ActiveForums 3.7
|
|
|
|
|
 |  |  |
|