如何阅读便携式类库中的资源文件? [英] How to read a resource file within a Portable Class Library?

查看:137
本文介绍了如何阅读便携式类库中的资源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用了Windows Phone应用程序可移植库。在同一便携图书馆,我有一对夫妇内容文件(生成操作的=的内容的)。



我的开创了便携式图书馆这是应该流回到我的内容文件中的类的DataReader 。不过,下面我的代码,我一直从 GetManifestResourceStream 找回。我在做什么错了?



 公共类的DataReader 
{
公共静态流GetStream(串码)
{
路径字符串=的String.Format(./数据/代码 - {0} .DAT,代码);
返回Assembly.GetExecutingAssembly()GetManifestResourceStream(路径);
}
}


解决方案

您路径是错误的。您使用的斜线,但在嵌入式清单资源名称斜线在生成过程中转化为周期。
也取决于你的PCL目标平台上,你可能甚至无法调用 Assembly.GetExecutingAssembly()



下面是你可以做什么:

  VAR总成= typeof运算(AnyTypeInYourAssembly).GetTypeInfo()汇编。 

//使用这个帮助援助弄清楚实际清单资源的名称是什么。
的String [] =资源assembly.GetManifestResourceNames();

//一旦你弄清楚了名字,把它作为这里的参数。
流流= assembly.GetManifestResourceStream(Some.Path.AndFileName.Ext);


I have a Portable Library which I am using for a Windows Phone application. In that same Portable Library, I have a couple of content files (Build Action = Content).

I created a class DataReader in the Portable Library which is supposed to return me a stream to the content file. However, with the code below I am consistently getting back null from GetManifestResourceStream. What am I doing wrong?

public class DataReader
{
    public static Stream GetStream(string code)
    {
        string path = string.Format("./data/code-{0}.dat", code);
        return Assembly.GetExecutingAssembly().GetManifestResourceStream(path);
    }
}

解决方案

Your path is wrong. You're using slashes, but in the embedded manifest resource names slashes were converted to periods during the build. Also depending on your PCL targeted platforms, you may not even be able to call Assembly.GetExecutingAssembly().

Here is what you can do:

var assembly = typeof(AnyTypeInYourAssembly).GetTypeInfo().Assembly;

// Use this help aid to figure out what the actual manifest resource name is.
string[] resources = assembly.GetManifestResourceNames();

// Once you figure out the name, pass it in as the argument here.
Stream stream = assembly.GetManifestResourceStream("Some.Path.AndFileName.Ext");

这篇关于如何阅读便携式类库中的资源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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