How to include asset files in a .NET Standard 1.4

2019-08-26 09:09发布

Anyone know what is the correct way of referencing assets if the platform is ARM? In x86 I can use appX folder with linking but its not working on ARM

thanks

1条回答
我命由我不由天
2楼-- · 2019-08-26 09:47

If you want to get assets files from a .NET Standard library, you would need to mark the file as EmbeddedResource and Copy Always.

enter image description here

Then, you need to add a method to get these files in your .NET Standard library's class. For example:

namespace ClassLibrary1
{
    public class Class1
    {
        public static Stream GetImage()
        {
            var assembly = typeof(Class1).GetTypeInfo().Assembly;
            Stream stream = assembly.GetManifestResourceStream("ClassLibrary1.Assets.dog.jpg");
            return stream;
        }
    }
}

Please note this line assembly.GetManifestResourceStream("ClassLibrary1.Assets.dog.jpg");

The ClassLibrary1 is the namespace, the Assets is the Assets folder in the library project, the dog.jpg is the file.

enter image description here

In my sample, I put the image files in the Assets folder, if put it in root directory of project, then, this line should be like this:

assembly.GetManifestResourceStream("ClassLibrary1.dog.jpg");

You could use the following code to see all embedded resource:

foreach (var res in assembly.GetManifestResourceNames())
{
    System.Diagnostics.Debug.WriteLine("found resource: " + res);
}

After that, in your main project, you could call this method to get these files.

查看更多
登录 后发表回答