开发者

how to set Anonymous delegate as one parameter for InvokeSelf?

开发者 https://www.devze.com 2022-12-25 18:31 出处:网络
I tried to use InvokeSelf for silverlight to communicate with html: InvokeSelf can take object[] as parameter when making a call:

I tried to use InvokeSelf for silverlight to communicate with html: InvokeSelf can take object[] as parameter when making a call:

  ScriptObject Myjs;
  ScriptObject obj = Myjs.InvokeSelf(new object[] { element  }) as ScriptObject;

then I want make a call like with anonymous delegate:

Object obj;
obj = Invoke开发者_开发知识库Self(new object[] { element, delegate { OnUriLoaded(reference); } });

I got the error said: Cannot convert anonymous method to type 'object' because it is not a delegate type

How to resolve this problem?


The problem is that you cannot assign an anonymous method to an object. This is because the C# compiler doesn't know what delegate type should be used. You can fix the code by creating a delegate explicitly. Since this is Silverlight you can also use more succinct lambda expression notation:

obj = InvokeSelf(new object[] 
  { element, new Action(() => OnUriLoaded(reference)) }); 

That said, I'm not sure if it is possible to pass a delegate to JavaScript, but you should be able to compile the code now and try that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号