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

后端 未结 8 1709
夕颜
夕颜 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条回答
  •  萌比男神i
    2020-12-05 03:49

    First of all, retrieve your assembly like this (DataLoader being a class in your PCL assembly) :

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

    Add your file to portable resource and set build action to Embedded Resource.

    Then you can retrieve your ressource like this :

    string resourceNam= "to be filled";
    var assembly = typeof(DataLoader).GetTypeInfo().Assembly;
    var compressedStream = assembly.GetManifestResourceStream(resourceName));
    

    For example if I have a file logo.png in a folder "Assets/Logos" in an assembly "TvShowTracker.Helpers" I will use this code :

    string resourceNam= "TvShowTracker.Helpers.Assets.Logos.logo.png";
    var assembly = typeof(DataLoader).GetTypeInfo().Assembly;
    var compressedStream = assembly.GetManifestResourceStream(resourceName));
    

    Happy coding :)

提交回复
热议问题