locked
Scriptable Silverlight Object RRS feed

  • Question

  • Hi, im currently trying to make a chat app that would make WCF calls on app_exit, and i've read around that the only way to do so is through javascript, so i've been trying to make an application that triggers functions of my silverlight class, except i keep running into javascript errors. so right now i have it call a function on load via,

    <body onload="popout()">

      which calls this function here,

    function popout() {

    var plugin = document.getElementById("silverlightcontrol");

    plugin.content.thisismyobject.Pop(); }

     the problem is, it reports an error on plugin.content.thisismyobject.pop(); and i have it register the obj here

    namespace Testapp { [ScriptableType] public partial class MainPage : UserControl {

    public MainPage() { InitializeComponent();

    Loaded += new RoutedEventHandler(MainPage_Loaded); }

    [ScriptableMember()] void MainPage_Loaded(object sender, RoutedEventArgs e) {

    HtmlPage.RegisterScriptableObject("thisismyobject", this); }

    [ScriptableMember] public void Pop() { MessageBox.Show("pop"); }

      }

    }

     

    what am i doing wrong?

    Friday, June 11, 2010 2:06 PM

Answers

  • I don't even know if this code works anywhere, but I wrote it to solve the problem you are experiencing. http://forums.silverlight.net/forums/t/11508.aspx Side note... if I were doing a chat app today I'd focus on WCF duplex communications and handle disconnects on the server.
    Friday, June 11, 2010 9:13 PM

All replies

  •  1. Do not call in the <body onload> event. Call in some other event such as button click etc. Reason is when body onload is called, silverlight plugin is not created as <object> tag is usually inside the body tag. Something like

    <Input type="Button" value="Click to Pop" onClick="popout()"/>

     

    2. Make sure you have given right id for object which you are trying to use for getting the reference back. In your case it should be

    <object id="silverlightcontrol"

    I have tested and it is working absolutely fine. If you need sample I will be happy to upload.

     

    Mark as 'Answered' if it helps

    Friday, June 11, 2010 6:49 PM
  • thanks for replying, the problem with changing it to say, a button, is that i need the function to be called onunload, so that i can send a WCF call right before application exit.

    And yes that also works on my side, so we can rule out (2) for wrong object ID

     

    Friday, June 11, 2010 6:57 PM
  • I don't even know if this code works anywhere, but I wrote it to solve the problem you are experiencing. http://forums.silverlight.net/forums/t/11508.aspx Side note... if I were doing a chat app today I'd focus on WCF duplex communications and handle disconnects on the server.
    Friday, June 11, 2010 9:13 PM
  • http://csharperimage.jeremylikness.com/2009/11/silverlight-communication-three-ways-to.html
    Monday, June 14, 2010 12:55 AM
  • thanks that is quite useful. fortunately it just turned out that some of my code had wrong formatting n such and has been fixed

    Monday, June 14, 2010 12:08 PM