How to Use BinaryFormatter in windows phone 7

前提是你 提交于 2019-12-19 19:51:45

问题


How can I use BinaryFormatter in Windows Phone 7.

I use this using System.Runtime.Serialization.Formatters.Binary in service1.svc.cs but I can't use this Reference in windows phone 7.

Is there any solution to this?

My Code For DeserializeObject

 public static T DeserializeObject<T>(byte[] xml)
    {
        BinaryFormatter xs = new BinaryFormatter();
        MemoryStream memoryStream = new MemoryStream(xml);
        return (T)xs.Deserialize(memoryStream);
    } 

BinaryFormatter gives error in windows phone 7. So how can I Deserialize. What changes that I have to do in this code?


回答1:


As @driis mentioned, you cannot use BinaryFormatter on Windows Phone. You can use the binary encoding in a WCF endpoint (i.e., an endpoint whose binding is a custom binding with the BinaryMessageEncodingBindingElement and the HttpTransportBindingElement), and that will be supported on WP7. You just cannot use the binary formatter there.

Update following comment: Looking at your code, it's not only that code that needs to be changed - you need to change the service code as well, to serialize an object in a format which is supported in Silverlight. You can use the DataContractSerializer, with a binary reader / writer, or you can use another library which is supported in both cases. For example, the code below should work in both desktop and SL versions:

public static T DeserializeObject<T>(byte[] xml) 
{ 
    using (MemoryStream memoryStream = new MemoryStream(xml))
    {
        using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(
            memoryStream, XmlDictionaryReaderQuotas.Max))
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(T));
            return (T)dcs.ReadObject(reader);
        }
    }
}

And on the server:

public static byte[] SerializeObject<T>(T obj)
{
    using (MemoryStream ms = new MemoryStream())
    {
        using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(ms))
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(T));
            dcs.WriteObject(writer, obj);
            writer.Flush();
            return ms.ToArray();
        }
    }
}



回答2:


BinaryFormatter is not supported in Windows Phone 7.




回答3:


There is no library support for binary serialization on WP 7.1. as driis mentioned.

You should rather use XmlObjectSerializer instead, or even one of it's subclasses that support serialization of most common API objects, like contacts data, or such.

If you checked System.Runtime.Serialization namespace assembly (ex. in Visual Studio's object browser) you'd found hierarchy of adequate classes.

And why do you care about serialization method? XML serialization is more portable, more uniform and easier to read by a human.



来源:https://stackoverflow.com/questions/12376765/how-to-use-binaryformatter-in-windows-phone-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!