运行时的 Windows Phone 8 资源路径 [英] windows phone 8 resource path at runtime

查看:29
本文介绍了运行时的 Windows Phone 8 资源路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在我的 Windows Phone 8 应用程序中包含一些文件.应用程序将相应地从资源中获取这些文件,并根据算法读取或部分显示内容.我如何获得嵌入资源的路径?

I have to include some files with my windows phone 8 app. the app will accordingly get those file from resource and read or show partly the content according to the algorithm. how do i get the path to my embedded resource?

推荐答案

如果您在项目中包含文件,例如在项目的 Data 文件夹中(在我们的示例中为 json 文件)作为资源,则使用下一个代码获取内容来自该文件:

If you include file in you project, for instance in Data folder of your project (in our case json file) as a resource then use next code to get content from that file:

string content = string.Empty;
string resource_file = "Data/myfile.json";

if (IsLocalResourceFileExists(resource_file))
        {
            var resource = Application.GetResourceStream(new Uri(@"/YourProjectName;component/" + resource_file, UriKind.Relative));
            StreamReader streamReader = new StreamReader(resource.Stream, System.Text.Encoding.UTF8);
            content = streamReader.ReadToEnd();
            streamReader.Close();
        }

要检查资源中是否存在文件,请使用:

To check if file exist in resource use this:

public bool IsLocalResourceFileExists(string relativePath)
    {
        return Application.GetResourceStream(new Uri(@"/YourProjectName;component/" + relativePath, UriKind.Relative)) != null;
    }

将 YourProjectName 更改为您的项目名称.在此之后,conten 将 json 文件保存为字符串.

Change YourProjectName to name of you project. After this, conten holds json file as string.

希望对您有帮助

这篇关于运行时的 Windows Phone 8 资源路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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