Silverlight错误地传递JSON对象? [英] Silverlight passing JSON object incorrectly?

查看:114
本文介绍了Silverlight错误地传递JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Silverlight类,标有ScriptableType&我希望ScriptableMember能够将对象从Silverlight传递到javascript.当我调用JSON.stringify(在javascript中)时,我希望收到该对象的JSON表示形式,但我得到的只是{}

I have a Silverlight class marked with the ScriptableType & ScriptableMember and I expect to be able to pass the object from Silverlight to javascript. When I call JSON.stringify (in javascript) I expect to receive a JSON representation of the object but all I get is {}

该类定义为:

[ScriptableType()]
public class MyEvent
{
    [ScriptableMember(ScriptAlias = "eventContent")]
    public int EventContent { get; set; }
}

我像这样通过Silverlight传递对象:

I pass the object from Silverlight like this:

  var jsonObject = new MyEvent { EventContent = 1 };
  HtmlPage.Window.Invoke("publishValue", topic, jsonObject);

在javascript中,我正在执行以下操作:

And in javascript I'm doing the following:

 alert(topic);
 alert(jsonObject);
 alert(JSON.stringify(jsonObject));

当我使用调试器时,我只看到类型为ObjectjsonObject,但是调用alert(jsonObject)返回正确的类型,并且如果我访问属性jsonObject.eventContent,则会获得正确的值,但它没有JSON.stringify无法正确序列化.

When I use the debugger I only see the jsonObject as of type Object but the call alert(jsonObject) returns the correct type and if I access the property jsonObject.eventContent I get the correct value back, but it doesn't serialize correctly with JSON.stringify.

有人告诉我我做错了吗?

Anyone tell what I'm doing wrong?

在发送到javascript之前,我不需要在Silverlight中序列化该对象.

I don't want to have to serialize the object in Silverlight before sending to javascript.

欢呼

AWC

推荐答案

我已经设法解决了这个问题!

I've managed to solve the problem!

与其声明这样的对象以将其从Silverlight传递到javascript:

Instead of declaring an object like this for passing from Silverlight to javascript:

[ScriptableType()] 
public class MyEvent 
{ 
    [ScriptableMember(ScriptAlias = "eventContent")] 
    public int EventContent { get; set; } 
} 

我使用System.Json名称psace并创建一个像这样的JsonObject:

I use the System.Json namepsace and create a JsonObject like this:

  var ob = new JsonObject
  {
       {"eventContent", 1}
  };

查看System.Json.JsonObject的文档以获取更多信息.

Check out the documentation of System.Json.JsonObject for more info.

欢呼

AWC

这篇关于Silverlight错误地传递JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆