Silverlight loads zip Resources

Source: Internet
Author: User
Tags silver light

Silverlight uses standard ZIP compression for xap files. Therefore, we can use the zip decompression class provided by Silverlight to help us load resources, such as images and XML files. This function is helpful for loading resources in game development. Because the game resources are generally not small, this function enables dynamic loading and can be stored in an independent bucket. In this way, you can determine whether the resource file name can be used to download resources repeatedly. Happy silver believes that such a game resource solution is relatively effective.

Of course, this is just an example. After you understand it, you can load anything, because this function operates binary directly. Unless the stream you load has a problem, it works well.

A) It is located in system. Windows. Resources. streamresourceinfo. It saves your resources as a stream. Help you prepare the native Silverlight zip solution class to restore resource files.

B) The solution is to decompress and restore resources through the system. Windows. application. getresourcestream method.

The following is the complete code for downloading a zip-pressurized resource file through WebClient, and then restoring the resource and using it:

 1:  public class Loader
 2:      {
 3:          public delegate void loadComplateEventHandler(List<StreamResourceInfo> resources);
 4:          public event loadComplateEventHandler loadComplate;
 5:          public delegate void loadProgressChangedHandler(int prosent);
 6:          public event loadProgressChangedHandler loadProgressCheanged;
 7:  
 8:          WebClient wc;
 9:  
10:          public Loader()
11:          {
12:              wc = new WebClient();
13:              wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
14:          }
15:  
16:          void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
17:          {
18:              if (loadProgressCheanged != null)
19:              {
20:                  loadProgressCheanged(e.ProgressPercentage);
21:              }
22:          }
23:  
24:          public void loadPng(string uri)
25:          {
26:              wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wcPng_OpenReadCompleted);
27:              wc.OpenReadAsync(new Uri("../" + uri, UriKind.Relative));
28:          }
29:  
30:          public void loadPngAbs(string uri)
31:          {
32:              wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wcPng_OpenReadCompleted);
33:              wc.OpenReadAsync(new Uri(uri, UriKind.Absolute));
34:          }
35:  
36:          public void loadWma(string uri)
37:          {
38:              wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wcWma_OpenReadCompleted);
39:              wc.OpenReadAsync(new Uri("../" + uri, UriKind.Relative));
40:          }
41:  
42:          public void loadWmaAbs(string uri)
43:          {
44:              wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wcWma_OpenReadCompleted);
45:              wc.OpenReadAsync(new Uri(uri, UriKind.Absolute));
46:          }
47:  
48:          void wcWma_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
49:          {
50:              if (e.Error == null)
51:              {
52:                  complate(e.Result, ".wma");
53:              }
54:          }
55:  
56:          void wcPng_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
57:          {
58:              if (e.Error == null)
59:              {
60:                  complate(e.Result, ".png");
61:              }
62:          }
63:  
64:          void complate(System.IO.Stream result, string ext)
65:          {
66:              List<StreamResourceInfo> res = new List<StreamResourceInfo>();
67:              StreamResourceInfo zipResourceInfo = new StreamResourceInfo(result, null);
68:              for (int i = 1; i < 100; i++)
69:              {
70:                  StreamResourceInfo imageResourceInfo = Application.GetResourceStream(zipResourceInfo,
71:                      new Uri(i.ToString() + ext, UriKind.Relative));
72:                  if (imageResourceInfo != null)
73:                  {
74:                      res.Add(imageResourceInfo);
75:                  }
76:                  else
77:                  {
78:                      break;
79:                  }
80:              }
81:              loadComplate(res);
82:          }
83:      }
84:  

The above operations may seem a bit complicated. Happy silver light will briefly explain the key points and focus on the operations in the complate method.

A) I first defined a list <streamresourceinfo> res = new list <streamresourceinfo> (). This RES is used to prepare the file stream after decompression and restoration. A streamresourceinfo is a file. This indicates how many elements are used for each file.

B) zipresourceinfo in the above Code is a stream. It contains a complete zip binary file stream. Because the files in the Happy silver zip resource are named by the number Int. Therefore, a for statement will appear to enumerate these file names, and then call application. getresourcestream (zipresourceinfo, Uri); in this way, you can read the resource files in the stream. Note that urikind in Uri uses relative because the loaded resource file is the resource location already in the silverlihgt cache assembly.

Next, happy bank will write a simplified code to help you better understand the focus of this article:

1:  void complate(System.IO.Stream result, string ext)
2:  {
3:      StreamResourceInfo pngResource = new StreamResourceInfo(result, null);
4:      StreamResourceInfo pngfile = Application.GetResourceStream(pngResource, new Uri("1.png", UriKind.Relative));
5:      System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage();
6:      bi.SetSource(pngfile.Stream);
7:  }
8:  

How. Simple. In this case, your 1.png can be returned as a bitmapimage. Then you can display the image in your image. Source = Bi.

Next we will explain how to use the complete function class on Happy silver: The following is the loading method.

 1:              //loadmap
 2:              Loader pl = new Loader();
 3:              pl.loadComplate += (res) =>
 4:              {
 5:                  Dictionary<int, StreamResourceInfo> maps = new Dictionary<int, StreamResourceInfo>();
 6:                  int counter = 0;
 7:                  for (int i = 0; i < res.Count; i++)
 8:                  {
 9:                      counter++;
10:                      maps.Add(counter, res[i]);
11:                  }
12:              };
13:              pl.loadProgressCheanged += (ps) =>
14:              {
15:                  var mp = App.Current.RootVisual as MainPage;
16: MP. textblock3.text = "? Figure? Lower? Carrier? "+ PS. tostring () +" % ";
17:              };
18:              pl.loadPng("res/map2.zip");
19: // pl. loadpngabs ("http: // domain name/map1.zip"); // This method loads the ZIP file on the Internet address.
20:  

The following code shows how to use the loaded maps:

1:  public BitmapImage GetImageFromStream(StreamResourceInfo stream)
2:  {
3:      BitmapImage bi = new BitmapImage();
4:      bi.SetSource(stream.Stream);
5:      stream.Stream.Position = 0;
6:      return bi;
7:  }
8:  
1:  body.Source = GetImageFromStream(mapUri);
2:  

The above body is actually an image control.

Okay. So far, I have explained most of the functions once. If the independent storage area is used to prevent repeated download of resources, Happy silver will be explained in the next article.

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.