Silverlight (WP7) loading a resource

左心房为你撑大大i 提交于 2019-12-24 10:15:58

问题


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?


回答1:


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.




回答2:


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.




回答3:


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".




回答4:


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.



来源:https://stackoverflow.com/questions/5851673/silverlight-wp7-loading-a-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!