Skip to main content

Microsoft Silverlight

Answered Question Passing a EventHandler delegate between two Silverlight apps using Html Bridge RSS Feed

(0)

Deepthi.K
Deepthi.K

Member

Member

0 points

1 Posts

Passing a EventHandler delegate between two Silverlight apps using Html Bridge

I have a requirement where a EventHandler delegate has to be passed from one silverlight app to the another silverlight app from where it will be invoked. Both of them communicate using the htmlbridge.

This is the code for my sample.

Silverlight App 1:
[ScriptableType]
public class EventDelegate
{       
     public void TestEventDelegate(EventHandler callback)
     {           
         callback(this, new EventArgs());
     }
}

Silverlight App 2:

public class TestDelegate
{
     ScriptObject so;
    // This so represents the TestEventDelegate of the first app..
     public TestDelegate(ScriptObject so)
     {
         this.so = so;
     }

     public void Test()
     {
          so.Invoke("TestEventDelegate", new EventHandler(MyEventHandler));
     }

     public void MyEventHandler(object sender, EventArgs e)
     {
          // Came here         
     }
  }

The problem now is if i replace the following line in the TestEventDelegate

callback(this, new EventArgs());  with

callback(this, null);

MyEventHandler in 2nd app is invoked but if new EventArgs() is passed as the second argument this function call fails with the following exception.

 System.ArgumentException: This object cannot be converted to the specified type 'System.EventArgs'.
Parameter name: targetType
   at System.Windows.Browser.ScriptObject.ConvertTo(Type targetType, Boolean allowSerialization)
   at System.Windows.Hosting.ScriptingInterface.GetScriptParamValueForType(ScriptParam scriptParam, Type desiredType)
   at System.Windows.Hosting.ScriptingInterface.ConvertFromScriptParams(ParameterInfo[] parameters, ScriptParam[] args)
   at System.Windows.Browser.ManagedObjectInfo.ScriptDelegate.Invoke(ManagedObject obj, InvokeType invokeType, ScriptParam[] args)
   at System.Windows.Browser.ManagedObjectInfo.Invoke(ManagedObject obj, InvokeType invokeType, String memberName, ScriptParam[] args)
   at System.Windows.Browser.ManagedObject.Invoke(InvokeType invokeType, Int32 memberID, ScriptParam[] args)
   at System.Windows.Hosting.ScriptingInterface.InvokeScriptableMember(ManagedObject obj, InvokeType invokeType, Int32 memberID, ScriptParam[] args, ScriptParam& pResult)
   at System.Windows.Hosting.ManagedHost.InvokeScriptableMember(IntPtr pHandle, Int32 nMemberID, Int32 nInvokeType, Int32 nArgCount, ScriptParam[] pArgs, ScriptParam& pResult, ExceptionInfo& pExcepInfo) 

 How do i pass in the second argument to avoid this exception? Is this the right way for passing delegates between silverlight applicaitons? Is there any other way of passing the delegates ? any help is really appreciated.

Mog Liang - MSFT
Mog Lian...

All-Star

All-Star

15864 points

1,541 Posts

Answered Question

Re: Passing a EventHandler delegate between two Silverlight apps using Html Bridge

Silverlight 3 has a new function called LocalMessaging, this function allows communcation between different silverlight in same web page or in different page.

here are samples and aritcle about LocalMessaging

http://blogs.msdn.com/pstubbs/archive/2009/04/02/silverlight-3-what-s-new-with-local-messaging.aspx

http://blogs.msdn.com/jstegman/archive/2009/03/23/local-messaging-samples.aspx

and a thread discuss "local messaging" in silverlight 2

http://silverlight.net/forums/t/92763.aspx

Mog Liang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities