Application.GetResourceStream called on a Content

2019-01-15 08:18发布

问题:

Here is the task-related part of the VS2010 project (Windows Phone) structure:

The code is being executed from DummyMediaLibProvider.cs:

public class DummyMediaLibProvider: IMediaLibProvider
{
    ...
    StreamResourceInfo albumArtPlaceholder = 
        Application.GetResourceStream(
            new Uri("../Images/artwork.placeholder.png", UriKind.Relative));

artwork.placeholder.png Build Action is set to Content.

Still, whenever I run the code, Application.GetResourceStream returns null.

What may be the reason for the resource not being read to memory?

I have attempted to delete obj directory of the project, did Clean and Rebuild, but so far nothing helped.

Update:

If I apply Build Action: Resource to artwork.placeholder.png, I can get the resource stream ok though.

P.S. This is not the duplicate of Application.GetContentStream returns null for content Uri since the last had the extension (particurarly .xml) related problem.

回答1:

The path supplied Application.GetResourceStream isn't relative to the position of the class, but relative to the application package.

StreamResourceInfo albumArtPlaceholder = 
    Application.GetResourceStream(
        new Uri("Images/artwork.placeholder.png", UriKind.Relative));

Would be the correct path. You can also try with a full pack URI. (see MSDN)

And finally, Resource would be the correct Build Action for this.