Silverlight的从'哑巴'服务器负载参考数据点播 [英] Silverlight Loading Reference Data On Demand from a 'dumb' server

查看:94
本文介绍了Silverlight的从'哑巴'服务器负载参考数据点播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有30万字的列表,并与它们至极发生频率的文本文件。每一行的格式为字:FequencyOfOccurence。 我希望这些信息是从C#code访问。我可以在c名单,因为实在是太长了,我不知道如何去从服务器上的文件访问它并不难$ C $。理想的情况是我最想要的仅被下载的信息,如果它是用来(为了节省带宽),但是这不是一个高优先级的文件不是太大,互联网速度总是不断增加。 它并不需要是可用的结合。 该信息并不需要进行编辑,一旦该项目已建成。

I have a text file with a list of 300,000 words and the frequency with wich they occur. Each line is in the format Word:FequencyOfOccurence. I want this information to be accessible from within the C# code. I can't hard code the list since it is too long, and I'm not sure how to go about accessing it from a file on the server. Ideally I'd ideally like the information to be downloaded only if it's used (To save on bandwidth) but this is not a high priority as the file is not too big and internet speeds are always increasing. It doesn't need to be useable for binding. The information does not need to be editable once the project has been built.

推荐答案

下面是另一种选择。邮编文件并把它贴在ClientBin目录旁边apllication XAP。然后,在在需要内容的应用程序的角度做这样的事情: -

Here is another alternative. Zip the file up and stick it in the clientBin folder next to the apllication XAP. Then at the point in the app where the content is needed do something like this:-

 public void GetWordFrequencyResource(Action<string> callback)
 {
     WebClient client = new WebClient();
     client.OpenReadAsync += (s, args) =>
     {
       try
       {
         var zipRes = new StreamResourceInfo(args.Result, null)
         var txtRes = Application.GetResourceStream(zipRes, new Uri("WordFrequency.txt", UriKind.Relative));
         string result = new StreamReader(txtRes.Stream).ReadToEnd();

         callback(result);
       }
       catch
       {
         callback(null);  //Fetch failed.
       } 

     }
     client.OpenReadAsync(new Uri("WordFrequency.zip", UriKind.Relative"));
 }

用法: -

Usage:-

 var wordFrequency = new Dictionary<string, int>();
 GetWordFrequencyResource(s =>
 {
    // Code here to burst string into dictionary.
 });
 // Note code here is asynchronous with the building of the dictionary don't attempt to 
 // use the dictionary here.

以上code允许您将文件存储在一个有效的zip格式,但不是在XAP本身。因此,您可以在按需下载。它利用了这样一个事实:XAP 一个zip文件,以便 Application.GetResourceStream ,其目的是从XAP文件拉的资源可用于一个zip文件。

The above code allows you to store the file in an efficient zip format but not in the XAP itself. Hence you can download it on demand. It makes use of the fact that a XAP is a zip file so Application.GetResourceStream which is designed to pull resources from XAP files can be used on a zip file.

顺便说一句,我实际上并不建议你使用一本字典,我只是用一个字典作为简单的例子。在现实中,我会想象该文件正在有序。如果是这样的话,你可以使用 KeyValuePair&LT;字符串,INT&GT; 为每个条目,但创建,保存它们在数组中的自定义集合类型或列表然后使用一些二进制搜索方法索引到它。

BTW, I'm not actually suggesting you use a dictionary, I'm just using a dictionary as simple example. In reality I would imagine the file is in sorted order. If that is the case you could use a KeyValuePair<string, int> for each entry but create a custom collection type that holds them in an array or List and then use some Binary search methods to index into it.

这篇关于Silverlight的从'哑巴'服务器负载参考数据点播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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