Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Passing a EventHandler delegate between two Silverlight apps using Html Bridge
1 replies. Latest Post by Mog Liang - MSFT on May 15, 2009.
(0)
Deepthi.K
Member
0 points
1 Posts
05-12-2009 4:30 AM |
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
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 Lian...
All-Star
15864 points
1,541 Posts
05-15-2009 4:53 AM |
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