Silverlight (WP7) loading a resource

2019-08-13 02:37发布

I have the following code. It's not finding my resource:

    string filename = "Resources/Functions.plist";
    Uri fileUri = new Uri(filename, UriKind.Relative);
    StreamResourceInfo sr = Application.GetResourceStream(fileUri);

But after the above executes, sr is null. Not good.

I do have a file named "Functions.plist" in a directory named "Resources", which is a subdirectory of my project directory. When I right click it in the solution explorer, I see its build action as "Resource" and its copy to output directory as "copy if newer".

Here is the portion of the .csproj file that loads it, or at least I think it does:

<ItemGroup>
   // AppManifest and WMAppManifest here
<Resource Include="Resources\**">
   <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Resource>

What could be wrong?

4条回答
倾城 Initia
2楼-- · 2019-08-13 02:46

What I usually do is load a random image in the designer, and then I copy the auto-generated path from the XAML code and use it as a template for my own .CS code.

Of course, if it is not an image, I use the path as a template for other objects.

It might also help if you select the "Always Copy" option.

查看更多
男人必须洒脱
3楼-- · 2019-08-13 02:46

I think you can get resouces like this.

string filename = "Resources\\Functions.plist";
Uri fileUri = new Uri(filename, UriKind.Relative);
StreamResourceInfo sr = Application.GetResourceStream(fileUri);

Because WP7's file seperator is slash, not back slash.

I hope this could help.

查看更多
小情绪 Triste *
4楼-- · 2019-08-13 02:51

Turns out what I had to do was use the following to include the files in the project:

<ItemGroup>
  <None Include="Properties\AppManifest.xml">
    <SubType>Designer</SubType>
  </None>
  <None Include="Properties\foo.plist" />
  <None Include="Properties\WMAppManifest.xml">
    <SubType>Designer</SubType>
  </None>
  <Content Include="Resources\Functions.plist">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
  <Content Include="Resources\Help List.plist">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
            // and similarly for the other resource files
</ItemGroup>

I'm not sure, but the key may have been changing "Resource" to "Content".

查看更多
做个烂人
5楼-- · 2019-08-13 02:52

For resources you the uri format is slightly different that content, what you'll need is something like:

string filename = "/{yourAssemblyShortName};component/resources/functions.plist";

The first part tells the system which assembly to look for the resource in. If you don't know the assmebly name you should be able to find it in the project properties screen.

查看更多
登录 后发表回答