Skip to main content

Microsoft Silverlight

initParams in managed codeRSS Feed

(0)

JasonLP
JasonLP

Member

Member

16 points

14 Posts

initParams in managed code

I've seen a few code samples on how to read initParams from javascript, but so far nothing from C#.  In javascript, one would call sender.getHost() to get a reference to the SilverlightControl, and use that to read the initParams.

I can't find the equivalent in managed code.  Any suggestions?

Thanks,

-Jason-

jwilcox
jwilcox

Participant

Participant

1082 points

187 Posts

Microsoft

Re: initParams in managed code

Give this a try:

A string dictionary is available in the current WebApplication instance:
WebApplication.Current.StartupArguments

Namespace:
using System.Windows;

Hope this helps,
-Jeff

Jeff Wilcox [MSFT]
Silverlight
http://www.jeff.wilcox.name/blog/

This posting is provided "AS IS" with no warranties, and confers no rights.

cyberdyme
cyberdyme

Member

Member

24 points

7 Posts

Re: Re: initParams in managed code

Jeff - This does not seems to work the dictionary for StartupArguments always seems to be empty

y_makram
y_makram

Contributor

Contributor

6172 points

1,233 Posts

Re: Re: initParams in managed code

The access to the Silverlight host has been disabled for C# in the alpha release because of an issue related to Safari on Mac, it should be back in the next release. But you can access this value from JavaScript. Mark your class as scriptable, define an event and mark it as sciptable, register the instance of your class, and subscribe to the defined event. In the event handler get the Silverlight host and get the initparams. set initparams on your registered object.

I am in a hurry so there may be some ambiguity in what I say. If so please post a follow up question. If this is sufficient please select "mark as answer"

Thanks
Yasser Makram
http://www.silverlightrecipes.com
_____
Dont forget to click "Mark as Answer" on the post that helped you. If your question has not been answered, please post a followup question.

WJamesLord
WJamesLord

Member

Member

138 points

118 Posts

Re: Re: initParams in managed code

I think I have a solution.  First, get the ID of the Silverlight control as detailed in the "How can I acquire the Silverlight HtmlElement control?" post (see http://silverlight.net/forums/t/2173.aspx).  Then just invoke the following:

 

string params = HtmlPage.Document.GetElementByID(SilverlightId).GetAttribute("initParams") ;

 

cyberdyme
cyberdyme

Member

Member

24 points

7 Posts

Re: Re: Re: initParams in managed code

Yasser - What if you have multiple instances of  the same silverlight control on a page.. (How will the Java script to manage code work)

y_makram
y_makram

Contributor

Contributor

6172 points

1,233 Posts

Re: Re: Re: initParams in managed code

This will not be a problem. The event wiring will be instance specific. For example if you have two Silverlight controls with IDs control1, control2

You will first need to get a reference to the Silverlight control through getElementByID:

var control1 = document.getElementById("control1");
var control2 = document.getElementById("control1");
Then wire to the control specific handler
control1.content.myScriptableObject.myEvent = handler1;
control2.content.myScriptableObject.myEvent = handler2;
This approach is useful if you need instance specific handlers. If all you needis to set the initparams of the right instance, this could be easily done by submitting the control instance as the sender parameter of the instance
If this answers your question please select "Mark as Answer". If not please post a follow up question

 

Thanks
Yasser Makram
http://www.silverlightrecipes.com
_____
Dont forget to click "Mark as Answer" on the post that helped you. If your question has not been answered, please post a followup question.

luisabreu
luisabreu

Participant

Participant

1676 points

612 Posts

Re: Re: Re: initParams in managed code

hello.

well, i like the approach but i think that using events are overkill for this. I prefer to go with a property because it reduces the code I need to write.

jwilcox
jwilcox

Participant

Participant

1082 points

187 Posts

Microsoft

Re: Re: Re: initParams in managed code

Could you share the CreateSilverlight JavaScript you're using, which includes the initParams, etc.?  It should be a comma-deliminated list... I'd like to try to repro this on my Mac Mini... (I'm looking at the managed code WebApplication dictionary access, not the JavaScript solutions that've been discussed)

Thanks,
Jeff

Jeff Wilcox [MSFT]
Silverlight
http://www.jeff.wilcox.name/blog/

This posting is provided "AS IS" with no warranties, and confers no rights.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities