简体   繁体   English

Silverlight XAML对象的二进制序列化

[英]Binary serialization of Silverlight XAML object

I'm working on Silverlight application that needs to display complex 2d vector graphics. 我正在开发需要显示复杂的2d矢量图形的Silverlight应用程序。 It downloads zipped XAML file from the server, parses it (XamlRead) and injects to the layout root on the page. 它从服务器下载压缩的XAML文件,解析它(XamlRead)并注入页面上的布局根目录。 This works fine for fairly small xaml files. 这适用于相当小的xaml文件。 The problems is that I need to make it work with much bigger file (lots more content in it). 问题是我需要使用更大的文件(更多的内容)。 For example one of my uncompressed xaml files is 20 MB large and XamlRead method takes tool long to parse it. 例如,我的一个未压缩的xaml文件大20 MB,而XamlRead方法需要很长的工具来解析它。 My question is if is there a way to do all the parsing on the server side. 我的问题是,是否有办法在服务器端进行所有解析。 It would best to just store serialized binary output of XamlRead method as BLOB in the database. 最好只将XamlRead方法的序列化二进制输出存储为数据库中的BLOB。 However when I try to serialize it, I'm getting a message that "Canvas object is not marked as serializable". 但是,当我尝试序列化它时,我收到一条消息“Canvas对象未标记为可序列化”。 I will really appreciate any advices . 我真的很感激任何建议。

Silverlight doesn't have much binary serialization built in; Silverlight内置了很多二进制序列化; however, protobuf-net works on Silverlight and may help plug this gap. 然而, protobuf-net适用于Silverlight,可能有助于填补这一空白。 In the current build you can only really serialize types you control (due to adding attributes) - however, I'm in the middle of a big refactor to (among other things) add support for serializing types without attributes. 在当前版本中,您只能真正序列化您控制的类型(由于添加属性) - 但是,我正处于一个大型重构的中间(除其他外)添加对没有属性的序列化类型的支持。

I expect it to be about 2 more weeks before this is available as a (hopefully) stable build, but you're welcome to take a look at it then. 我希望它还有2个星期可以作为(希望)稳定的版本,但是欢迎你来看看它。

Note that you will still need to give it some help (telling it what you want it to serialize), but it may be useful. 请注意,您仍然需要提供一些帮助(告诉它您希望它序列化的内容),但它可能很有用。

In particular, the data format ("protocol buffers") is designed to be both dense and efficient to process, which should increase the parse speed. 特别是,数据格式(“协议缓冲区”)被设计为密集且高效的处理,这应该提高解析速度。 See here for more (numbers are from main .NET, not Silverlight) 请参阅此处了解更多信息(数字来自主.NET,而不是Silverlight)

I've found the SharpSerializer package very easy to use for fast binary serlization in Silverlight: http://www.sharpserializer.com/en/index.html . 我发现SharpSerializer软件包非常容易用于Silverlight中的快速二进制serlization: http//www.sharpserializer.com/en/index.html You do not need to use the Serializable attribute -- however it only serializes public members. 您不需要使用Serializable属性 - 但它只序列化公共成员。

If parsing is really the problem, it might help to use pre-compiled XAMLs called 'BAML'. 如果解析确实是问题,那么使用名为'BAML'的预编译XAML可能会有所帮助。 This is a binary representation of the XAML file. 这是XAML文件的二进制表示。 Since the binary format has a much much cheaper parser instead of the too generic XML, this helps a lot. 由于二进制格式具有更便宜的解析器而不是太通用的XML,这有很大帮助。 BAML is also used internally by the .NET compiler to generate more compact files. .NET编译器也在内部使用BAML来生成更紧凑的文件。

For more information, see also http://stuff.seans.com/2008/07/13/hello-wpf-world-part-2-why-xaml/ 有关详细信息,另请参阅http://stuff.seans.com/2008/07/13/hello-wpf-world-part-2-why-xaml/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM