How to read a resource file within a Portable Class Library?

后端 未结 8 1738
夕颜
夕颜 2020-12-05 03:26

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 = Cont

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 03:38

    Add your file to portable resource and set build action to Embedded Resource. For example files GB.png, US.png under folder CountryFlags.

    Add a getter function with code like this (here it's specific for our countryflag getter image).

    public class CountryFlags {
        public static Stream GetFlagStream(string countryIsoCode2ch)
        {
            var flagname = "Full.DLL.Name.CountryFlags.{0}.png";
            var rs = Assembly.GetExecutingAssembly().GetManifestResourceStream(
                       string.Format(flagname, countryIsoCode2ch));
    
            return rs;
        }
    }
    

    Here Full.DLL.Name is the part of generated portable library that is before .dll extension. (Note: Anything.Resources.dll is a bad name for a library because it gets ignored by Visual Studio at least when generating XAP etc.; instead for example Anything.PortableResource.dll will work).

提交回复
热议问题