locked
Does calling a ScriptableMember method on a Silverlight control from JavaScript create a new Instance of the control? RRS feed

  • Question

  • I have a Silverlight control with a method named DoSomething() decorated with the <ScriptableMember()> attribute. I then call this method successfully from JavaScript and proved by a little message box that apprears from the SL side that says "Method Called!".

    Point is all that works. The problem I am having is that prior to calling this method I build up an ObservableCollection on the Silverlight control containing 1..n FileInfo objects. This works fine too and builds up as I add files to it. Each time I add a file, a messagebox tells me the count from Silverlight (i.e. "Count = 2").

    Now the problem: when I call the method DoSomething() from JS and access that ObservableCollection the count = 0! To see what is going on I placed a message in the Silverlight control's constructor to see if it gets entered upon being called from JS, and indeed it does and appears to recreate the control.

    If this is the case it kind of makes sense that my ObservableCollection has a count = 0 because it is not the same control instance where I built up the FileInfo collection.

    So how in the world do I preserve the collection, and why would simply calling a method exposed to JS from Silverlight, recreate the control and not allow me to access it's given state? I don't want a new control, I need to manipulate it as-is. Or am I off base and doing something else wrong to cause this beahvior?

    Thoughts? Thanks!

    Thursday, March 8, 2012 3:58 PM

Answers

  • @Catherine - sorry for the delay, but thank you for the offer of actually looking at uploaded code. It is folks like you in our community that go the extra mile like that and make a positive difference, so thank you. Smile

    I ended up figuring this out and then expanding about it a bit on my blog. It turns out the instance registered was the culprit. The MSDN examples show registering a new instance of the type, but in my case I needed the actual instance of the Page Control itself which solved the problem.

    Get A Silverlight Control's Current Instance For Communicating Via The HTML Bridge:
    http://allen-conway-dotnet.blogspot.com/2012/03/get-silverlight-controls-current.html

    Wednesday, March 21, 2012 11:34 AM

All replies

  • Hi atconway,

    In order to resolve your issue as soon as possible, could you simply your project and upload it to skydrive?  And privide the link about it. I will download it and try to resolve it locally.

    Thank you for your support!

    Tuesday, March 13, 2012 3:26 AM
  • @Catherine - sorry for the delay, but thank you for the offer of actually looking at uploaded code. It is folks like you in our community that go the extra mile like that and make a positive difference, so thank you. Smile

    I ended up figuring this out and then expanding about it a bit on my blog. It turns out the instance registered was the culprit. The MSDN examples show registering a new instance of the type, but in my case I needed the actual instance of the Page Control itself which solved the problem.

    Get A Silverlight Control's Current Instance For Communicating Via The HTML Bridge:
    http://allen-conway-dotnet.blogspot.com/2012/03/get-silverlight-controls-current.html

    Wednesday, March 21, 2012 11:34 AM