传递对象通过服务器端asp.net code Silverlight的 [英] Pass objects to silverlight through server side asp.net code

查看:76
本文介绍了传递对象通过服务器端asp.net code Silverlight的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能通过asp.net服务器端code传递对象(序列化类或其他方式),以一个Silverlight控件?

Is it possible to pass objects (serializable classes or other ways) to a Silverlight control through asp.net server side code?

推荐答案

好了,它会涉及到序列化。记住 - 你的Silverlight客户端与服务器断开连接,就像浏览器与服务器断开连接

Well, it's going to involve serialization. Remember -- your Silverlight client is disconnected from the server, just like the browser is disconnected from the server.

有一个伟大的文章<一个href=\"http://www.silverlightshow.net/items/JSON-serialization-and-deserialization-in-Silverlight.aspx\"相对=nofollow>此处的JSON序列化,并从Silverlight的。以下是文章摘要:

There is a great article here on JSON serialization to and from Silverlight. Here is the summary from the article:

让我们先从一个简短的介绍的JSON是什么。它代表的Ĵ AVA ** S ** CRIPT 0 bject的 N 浮选,并作为XML的替代。下面是一个JSON文件一个简单的例子:

Let’s start with a short introduction of what JSON is. It stands for Java**S**cript Object Notation and is used as an alternative of the XML. Here is a simple example for a JSON file:

{姓:马丁,姓氏:Mihaylov} 单个对象

<$c$c>[{\"FirstName\":\"Martin\",\"LastName\":\"Mihaylov\"},{\"FirstName\":\"Emil\",\"LastName\":\"Stoychev\"}]对多个对象。

它看起来像一个数组。根据该序列化它可能看起来非常复杂的对象。

It looks like an array. Depending on the object that is serialized it could look really complicated.

为了要与序列化,我们DataContractJsonSerializer必须设置[DataContract]属性。将由序列所使用的型属性必须具有[数据成员]的属性。注意:要使用这些属性的引用System.Runtime.Serialization添加;

In order to be serializable with DataContractJsonSerializer we have to set a [DataContract] attribute. The properites that will be used by the serialization must have [DataMember] attributes. Note: To use these attributes add a reference to System.Runtime.Serialization;

[DataContract]
public class Person
{

    [DataMember]
    public string FirstName
    {
        get;
        set;
    }

    [DataMember]
    public string LastName
    {
        get;
        set;
    }

}

现在我们已经准备好开始与序列化。让我们创建一个需要我们的对象作为参数,并返回一个JSON格式的字符串的方法:

Now we are ready to begin with the serialization. Let's create a method that takes our object as an argument and returns a string in JSON format:

public static string SerializeToJsonString(object objectToSerialize)
{
    using (MemoryStream ms = new MemoryStream())
    {
        DataContractJsonSerializer serializer =
        new DataContractJsonSerializer(objectToSerialize.GetType());
        serializer.WriteObject(ms, objectToSerialize);
        ms.Position = 0;


        using (StreamReader reader = new StreamReader(ms))
        {
            return reader.ReadToEnd();
        }

    }

}

反序列化

public static T Deserialize<T>(string jsonString)
{

    using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))
    {

        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));


        return (T)serializer.ReadObject(ms);

    }

}

下面是这个样子,从客户端code:

Here is what this looks like from client code:

List<Person> persons = Deserialize<List<Person>>( jsonString );

这篇关于传递对象通过服务器端asp.net code Silverlight的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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