Get through-porting desktop applications developed by winforms to Ria developed by Silverlight (Part 1)

Source: Internet
Author: User
1. serializableattribute does not exist.

In Silverlight, serializableattribute is not included, but serialization is not affected without this mark. The reason for removing this feature mark is that a series of feature tags starting with XML are already used for serialization.

There are no difficulties in this change, mainly because it is troublesome and needs to be deleted one by one. Of course, if you do not want to delete it, the simplest way is to create a serializableattribute by yourself.

2. XML serialization of dictionary, replacing xmldocument with xelement, and changing parameters of convert. changetype

It means that it is not in Silverlight and there is no way to use xmlserializer to serialize dictionary <>. I directly implemented a dictionary capable of XML serialization,CodeAs follows:

 

    Public     Class  Xmlserdictionary  <  Tkey, tvalue  > : Dictionary  <  Tkey, tvalue  >  , Ixmlserializable {  # Region  Ixmlserializable members  System. xml. schema. XMLSCHEMA ixmlserializable. getschema (){  Return     Null  ;}  Void Ixmlserializable. readxml (system. xml. xmlreader reader) {xmldocument Doc  =     New  Xmldocument (); Doc. loadxml (reader. readouterxml ());  Foreach  (Xmlelement item  In  Doc. childnodes [  0  ]. Childnodes) {tkey key  =  (Tkey) convert. changetype (item. getattribute ( "  Key  "  ),  Typeof  (Tkey); tvalue  =  (Tvalue) convert. changetype (item. innertext,  Typeof  (Tvalue ));  This  . Add (Key, value );}}  Void  Ixmlserializable. writexml (system. xml. xmlwriter writer ){ Foreach  (  VaR  Item  In     This  ) {Writer. writestartelement (  "  Item  "  ); Writer. writeattributestring (  "  Key  " , Item. Key. tostring (); writer. writevalue (item. Value); writer. writeendelement ();}}  # Endregion  } 

 

However, in the readxml method, I used xmldocument. This object does not exist in Silverlight and can only be replaced by xdocument or xelement. The new Code is as follows:

 

   Void  Ixmlserializable. readxml (system. xml. xmlreader reader) {xelement ele  =  Xelement. parse (reader. readouterxml ()); Foreach  (  VaR  Item  In  Ele. descendants () {tkey key  =  (Tkey) convert. changetype (item. Attribute (  "  Key  "  ). Value,  Typeof  (Tkey ),  Null  ); Tvalue =  (Tvalue) convert. changetype (item. value,  Typeof  (Tvalue ),  Null  );  This  . Add (Key, value );}} 

 

Of course, if the file is not compatible with the previous XML serialization, you can use datacontact to directly serialize the dictionary <> (or even support JSON)

Pay attention to the Code modified above, and we find convert. the changetype parameter also changes, before convert. the changetype method supports two parameters, but only three parameters are supported in Silverlight. Therefore, null is input.

From this details, we can see that in order to reduce the size of Silverlight, Microsoft does not commit, and is streamlined to every method.

3. The find method of list <t> does not exist.

Find is an extension method provided before the emergence of LINQ, and Silverlight removes some similar extension methods to reduce the size. In order not to make too many changes to the original code, I had to implement the find method myself, as shown below:

 

 

   Public     Static  T find  <  T  >  (  This  Ienumerable  <  T  > List, func  <  T,  Bool  >  Comparison ){  Foreach  (T item  In  List ){  If  (Comparison (item )){  Return  Item ;}}  Return    Default  (T );} 

 

Of course, you can also use the first extension method instead.

4. Change directory. getfiles to enumeratefiles, and change getdirectories to enumeratedirectories.

This change is mainly caused by returning ienumerable <string> through enumeratefiles instead of the string [] of the previous getfiles method. Because there is a place where string [] must be used, otherwise it will have a great impact on the code, so we have to implement our own method:

 

   Public     Static     String [] Getfiles (  String  DIR ){  VaR  Names  =  Directory. enumeratefiles (DIR); List  <  String  >  LST  =     New  List  < String  >  (Names );  Return  Lst. toarray ();}  Public     Static     String  [] Getdirectories (  String  DIR ){  VaR  Names  = Directory. enumeratedirectories (DIR); List  <  String  >  LST  =     New  List  <  String  >  (Names );  Return  Lst. toarray ();} 

 

Of course, we recommend that you use the enumeratefiles method and enumeratedirectories method in most cases.

5. Read and Write ZIP files.

In. net, you can use the system. Io. Compression namespace to read and write ZIP files. sharpziplib can also be used.

In Silverlight, if you only want to read the content of the ZIP file, you can add the ZIP file to xap as the content and then use the following code to read the file:

 

 VaR  Zipfile  =  Application. getresourcestream (  New  Uri (  "  Lcadb/entries_ilcd.zip  " , Urikind. Relative ));  VaR  Xmlfile  =  Application. getresourcestream (zipfile,  New  Uri (  "  Locations_ilcd.xml  "  , Urikind. Relative); List  <  Location  >  LST  = Serializationhelper. fromxml  <  List  <  Location  >  (Xmlfile. Stream ); 

 

Use application. getresourcestream to access the compressed file. For more information, see:

Http://www.silverlightexamples.net/post/How-to-Get-Files-From-Resources-in-Silverlight-20.aspx

To write a zip file, no ready-made function libraries are available. Fortunately, some people have transplanted sharpziplib to Silverlight. See:

Http://slsharpziplib.codeplex.com/

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.