Mar
6
Written by:
New Age Solution
3/6/2010 6:06 PM
downloads/PassingLargeText.zipWe will look at passing large text to Silverlight using InitParams and then we will take a look at using Fiddler to see what gets passed into Silverlight.
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/PassingLargeText.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="4.0.41108.0" />
<param name="autoUpgrade" value="true" />
<param name="initparams" id="XamlInitparams" runat="server" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.41108.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
a>
object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px">iframe>div>
Download Source
In Web Application Project
1) In ASPX page create param named initparams and to be able to access code behind give it an id and set runat equals server.
2) In page load of code behind read the image file and IMPORTANT thing to note is to convert to base 64 string. Converting to base 64 string is important specially dealing with binary files and it would be the safest way to deal with any type of file. I had case where when object was serialized and it would fail to pass via initparams because of starge chars when object was serialized.
protected void Page_Load(object sender, EventArgs e)
{
string data = Convert.ToBase64String(File.ReadAllBytes(Server.MapPath("newagesolution_logo.png")));
XamlInitparams.Attributes["value"] = "MyTestFile=" + data;
}
In Silverlight Application
1) In the startup of App.xaml get InitParam which is key / value of dictionary and pass it to MainPage
private void Application_Startup(object sender, StartupEventArgs e)
{
string mytestFile = e.InitParams["MyTestFile"];
this.RootVisual = new MainPage(mytestFile);
}
2) In MainPage.xaml
public MainPage(string myTestFile)
{
InitializeComponent();
MemoryStream ms = new MemoryStream(Convert.FromBase64String(myTestFile));
BitmapImage src = new BitmapImage();
src.SetSource(ms);
Image img = new Image();
img.Source = src;
this.LayoutRoot.Children.Add(img);
}
In Fiddler
1) Use 127.0.0.1. ; Dot at the end of ip is not a typo. In order to use fiddler in localhost development you have to put the dot at the end of the ip.

2) In hexview you can see that it is passing initparms.
Conclusion
In this example I read image file BUT there are much better way to read image file. Image file was used to demonstrate the read of binary file and passing successfully into Silverlight. In real world, you might end up serializing DataContract and passing it to Silverlight. Why?
1) because sometimes you might not have access to the WCF service
2) in production say certain request was crashing the app and luckily say WCF traffic was logged. You can simply serialize and send the object into Silverlight for troubleshooting.
I tested as big as 5 MB file and then when things got bigger I simply zipped and unzipped in Silverlight so I never had need to go beyaond 5 MB file.