开发者

Dynamically passing parameters from ASPX host to Silverlight 4 xaml code-behind

Okay, first off, I'm new to Silverlight and am looking for someone to provide guidance as to whether the following solution is the prescribed way of going about this.

Yesterday I started working on a problem that, at first blush, seemed pretty simple and straightforward. I need to pass a few parameters from an ASPX code-behind, which hosts a Silverlight object tag, to the code-behind of one, or more, of the Silverlight user controls within the hosted Silverlight application.

So, after doing some research, this is the basic solution I developed...

I found out that an attribute can be added to the object tag called initParams, a comma delimited list of parameter names and values can be added to this attribute. Like so...

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/SampleApplication.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40624.0" />
<param name="autoUpgrade" value="true" />
<param name="initParams" value='DealerId=17' />
</object>

This is fine, except that the DealerId parameter is basically hard-coded in the object tag, not real useful.

The next thing that I did was replace this object tag with a literal control, and set the text of the literal control within the page's code-behind to the value of a StringBuilder (where I built up the full object tag along with dynamically adding the correct DealerId value). In the following example, the DealerId is hard-coded, but you get the idea.

var sb = new StringBuilder();
            sb.Append(@"<object data=""data:application/x-silverlight-2,"" type=""application/x-silverlight-2"" width=""90%"" height=""80%"">");
            sb.Append(@"<param name=""source"" value=""ClientBin/Ascend.SilverlightViewer.xap""/>");
            sb.Append(@"<param name=""onError"" value=""onSilverlightError"" />");
            sb.Append(@"<param name=""background"" value=""white"" />");
            sb.Append(@"<param name=""minRuntimeVersion"" value=""3.0.40624.0"" />");
            sb.Append(@"<param name=""autoUpgrade"" value=""true"" />");
            sb.Append(@"<param name=""initParams"" value='");
            sb.Append(@"ServiceUrl=");
            sb.AppendFormat("http://{0}{1}", Request.Url.Authority, ResolveUrl("~/ReportService.svc"));
            sb.Append(@",DebugMode=Full");
            sb.AppendFormat(@",DealerId={0}' />", 40);
            sb.Append(@"</object>");

            litObjectTag.Text = sb.ToString();

My goal, if this initial design is sane, is to then pull this object tag creation into a server control, which will have a DealerId property, which in turn will be set within the hosts code-behind.

At this point, I have the host dynamically adding parameter values to the object tag's initParams attribute, the next step is to get these values and leverage them within the hosted Silverlight application.

I found a few articles to help out with this; I'm creating a public dictionary within the App.xaml.cs, and setting it within the Application_Startup event...

public IDictionary<string, string> InitConfigDictionary;

private void Application_Startup(object sender, StartupEventArgs e)
{
  InitConfigDictionary = e.InitParams;
  this.RootVisual = new MainPage();
}

Now, I can access this public dictionary from the code-behind of any .xaml user control, like this...

App app = (App)Application.Current; var dealerId = app.InitConfigDictionary["DealerId"];

This design works just fine, I'm just looking for some guidance, since I'm开发者_C百科 new to Silverlight. Once again, the implementation works, but it seems like a whole lot of work to go through just to pass a dynamic value from the host to the .xaml files.

Because I'm new to Silverlight, I'm hoping that someone with more experience can say that either:

a) Patrick, you're insane, why are you going through all this work when clearly in Silverlight you would accomplish this through the use of "xxxxxx".

b) Yeah, Patrick, it's a drag, but this design is basically what you have to do in Silverlight.

Once again, any guidance that could be provided would be much appreciated, thanks. - PWK


You dont need to parse the whole thing i believe. You can call the code behind from following way from aspx page

<param name="initParams" value="<%= BuildInitParams()%>" />

And in your code behind have

    public string BuildInitParams()
    {
        //Dynamically build the string here;
    }

hope this helps.

Rakibul


This may be a bit of an old question, but I remember having a similar issue and I just came up with a resolution of sorts, at least for my needs.

First, I added code behind page for my .aspx page (hosting my Silverlight application). In the past I had been hard-coding my key/value pairs into the initParams field, like so:

<param id="initParams" runat="server" name="initParams" value="param1=foo,param2=bar" />

I wanted a solution where I could store these values in a config file, however. So I added an section to my config file with the values I wanted. I cleared out the value attribute in the param tag on the aspx page, and instead I dynamically pull items from the and put them into the value attribute dynamically at run time, using the Page_Load in the ASPX page:

protected void Page_Load(object sender, EventArgs e)
        {
            initParams.Attributes["value"] += "userId=" + Page.User.Identity.Name;
            foreach (var key in ConfigurationManager.AppSettings.AllKeys)
            {
                initParams.Attributes["value"] += "," + key + "=" + ConfigurationManager.AppSettings[key];
            }
        }

From there, within the App.xaml.cs page of the Silverlight application, I have added this code:

private void Application_Startup(object sender, StartupEventArgs e)
        {
            this.RootVisual = new MainPage();
            // Take parameters and store them in application resources
            foreach (var data in e.InitParams)
            {
                this.Resources.Add(data.Key, data.Value);
            }
        }

Once the Silverlight application loads, it stores these in the Resources object, and then in code when needed I can refer to them using:

Application.Current.Resources["param1"].ToString();

My particular use for this was an endpoint address for a service URL. I wanted this to be something I could change in a config file, allowing other IT people to update the service address without me having to rebuild/redeploy. I could have hard-coded this into the initParams value attribute of the ASPX page, but making changes there is not intuitive or easy to read. I prefer the config file approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜