This Article describes How to pass Init Parameters in different Silverlight xap files when using a Modular approach
Generally when we want to pass Init parameters into Silverlight object at runtime, we follow something like this.
//App.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage(e.InitParams);
}
//MainPage.xaml.cs
public partial class MainPage : UserControl
{
public MainPage(IDictionary<string, string> initParams)
{
var myValue = initParams["MyKey"];
InitializeComponent();
}
}
TestPage.aspx
<form id="form1" runat="server" style="height: 100%">
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
width="100%" height="100%">
<param name="source" value="ClientBin/SilverlightApp.xap" />
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="4.0.50826.0" />
<param name="autoUpgrade" value="true" />
<param name="initParams" value="MyKey=Effectlabs" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.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>
</form>
And it is the same piece of code which is found almost every where if you google on how to use Init Parameters with Silverlight.
The code seems to perfect in almost all the cases. However, it would need a reconsideration in few cases.
Imagine a situation when you are working with MEF and you want to pass the Init Parameters into the second xap file which is called by the primary xap file.
Seems to be tedious as:
There is no role of App.xaml.cs in the second ”Module”, meaning there is no startup object there, or in other words an application can get to use only one App.xaml.cs as a time.
In the App.xaml.cs of MEF based Project, we generally call new Bootstrapper() which then calls “Shell” whose constructor looks something like this
[ImportingConstructor]
public Shell([Import]IModuleManager modulemanager)
{
InitializeComponent();
So one cannot manage to insert the above piece of code anywhere in the modular approach
Now the question is how we achieve the task of sending the Init Parameters to the second module if we don’t even have its access in first module.
In past few day while working with MEF, I observed that the resources inside “App.xaml.cs” of the Main Module are shared between all the other modules. Now there is something called “Application.Current.Host.InitParams” which I guessed must also have been shared along with the other resources, but in this approach, I couldn’t have set something inside it manually, and then I recollected that we didn’t even used to do it in the regular non modular approach, but something automatically gets into it when the parameters are passed
and the guess was right, I was able to access the Init Parameters by this way
1: public MainPage()
2: {
3: var initParam = Application.Current.Host.InitParams;
4: if(initParam.ContainsKey("MyKey")) InitParameterValue = initParam["MyKey"];
5: InitializeComponent();
6: DataContext = this;
7: }



Hence making the things pretty simpler, we finally have two questions answered:
1) How to pass Init Parameters to other modules of MEF based project ?
Passing Init Parameters from one module to other is not exactly a task, but it’s a matter of accessing it.
2) is there any other method of passing the Init Parameters then the regular traditional one ?
It’s a Myth that the method demonstrated above (very top) or on almost every site to pass Init Parameters is the only way to do it, rather the constructor of Root Visual does not necessarily need to accept an I-Dictionary in order to prove it accepts Init Parameters.
I have attached the sample project used in this demonstration.
0217100e-acc3-4c54-8684-6f986def571f|1|5.0
Categories:
Prism, MEF, Silverlight
27. November 2011
Tags:
Init parameters, Silverlight, Modular Approach, MEF, Prism