Hello folks,
Today I will tell you how we can pass data from Aspx page to silverlight application . I will also discuss about the scenario where we required to pass string in double quotes (Title of the blog is "Experts Comment")
Let us take a simple example and try to show you the working.
When we created a Silverlight application by default a test aspx page is created. In that you will find an object tag that contain the address of the XAP file.
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/PassingDataToSilverlightPage.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40818.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight"
style="border-style:none" />
</a>
</object>
To pass the data from page to Silverlight, we need to add an extra <param/> tag with name initParams.
<param name="initParams"value="Name=ExpertsComment,Tile=Title is Experts Comment" />
This initParams is accessible inside the App.Xaml.cs in Application_StartUp Event.
The data that is passed to SilverLight is in key value pair of string. Next, we need to store this information in some variable to make accessible across the pages.
public Dictionary<string, string> PageData = new Dictionary<string, string>();
private void Application_Startup(object sender, StartupEventArgs e)
{
if (null != e.InitParams)
foreach (var item in e.InitParams)
this.PageData.Add(item.Key, item.Value);
}
this.RootVisual = new MainPage();
Now to access this data on the page, we need to access the PageData parameter inside xaml.cs of page.
Let me create a couple of TextBlock's on the page and bind the Name and Url to the same. Add set the Text property from the code behind.
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="Name: "></TextBlock>
<TextBlock x:Name="txtblkName" Margin="40,0,0,0"/>
<TextBlock Text="Url: " Margin="0,15,0,0"></TextBlock>
<TextBlock x:Name="txtblkUrl" Margin="40,15,0,0"/>
</Grid
public partial class MainPage : UserControl
public MainPage()
InitializeComponent();
App currentApp = (App)Application.Current;
this.txtblkName.Text = currentApp.PageData["Name"];
this.txtblkUrl.Text = currentApp.PageData["Url"];
This is all that you need to do to pass the data from Aspxpage to silverlight page.
Quite simple, isn't it. Now lets us see how we can pass the string that already has double quotes in it.
If we directly pass the data like we did earlier in the code or other two variants as show below, we will not get the desired result.
<param name="initParams" value="Name=ExpertsComment,Tile=Title is Experts Comment" />
<param name="initParams" value="Name=ExpertsComment,Tile=Title is ""Experts Comment"" />
<param name="initParams" value="Name=ExpertsComment,Tile=Title is \"Experts Comment\"" />
This is because the value part of initParams looks for all the data between double quotes and ignore the rest of it. So to achieve the result we need to do a little workaround. Instead of pass double quote's, we will pass its encoded value and decode back inside the page.
<param name="initParams"value="Name=ExpertsComment,Tile=Title is %22Experts Comment%22" />
Now write the code in App.xaml.cs Application_Startup
this.PageData.Add(item.Key, Uri.UnescapeDataString(item.Value));
That is all that you need to do.
Happy Learning J