开发者

Loading file in silverlight

开发者 https://www.devze.com 2023-01-23 05:12 出处:网络
I would like to load a custom file put as a resource file in my silverlight application, but the FileStream doesn\'t works (since I must stay in a partial trust environnment).

I would like to load a custom file put as a resource file in my silverlight application, but the FileStream doesn't works (since I must stay in a partial trust environnment).

Is there any solution to load my file? (it is a binary serialized data).

UPDATE Answer found :

  • I put my file as a "Resource" (not embedded neither content or anything else)
  • Loaded it like this :

    StreamResourceInfo info = Application.GetResourceStream(new Uri(@"/Utilitaires;component/Resources/" + name, UriKind.Relative));

And then using the "info.Stream" property.

Now, I have an other asking. By doing like this, the file is added to the assembly (to the exe/dll), and make it a bit bigger.

But since these datas need to be loaded at the same time as the assembly, should I let them as a resource, or use another method to load them separatly? (and what should be the method? I need it to work in local as well a开发者_开发问答s on a server).

Thanks,

KiTe


Since you need the resource at the same time as you load the assembly then the only other reason to place the file outside the Xap would be to allow the file to be modified without modifying the Xap.

Personally I would include the file as "Content" rather than "Resource". This means that the file ends up as an entry in the Xap (which is just a Zip file) rather than inside a dll.

You still use GetResourceStream to access it but the Url becomes something like:-

new Uri(@"/Assets/" + name, UriKind.Relative)

Where Assets is a folder you create in your project to store additional files, also name should include a file extension.

Using this approach kind of gives you the best of both worlds. The file is included in the Xap but if for some reason the file content needs to be modified the Xap can be opened as a Zip file and the file replaced.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号