简体   繁体   English

Silverlight (WP7) 加载资源

[英]Silverlight (WP7) loading a resource

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.但是上面执行之后,sr就是null。 Not good.不好。

I do have a file named "Functions.plist" in a directory named "Resources", which is a subdirectory of my project directory.我在名为“Resources”的目录中确实有一个名为“Functions.plist”的文件,该目录是我的项目目录的子目录。 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".当我在解决方案资源管理器中右键单击它时,我将其构建操作视为“资源”,并将其复制到 output 目录中作为“如果较新则复制”。

Here is the portion of the.csproj file that loads it, or at least I think it does:这是加载它的 .csproj 文件的一部分,或者至少我认为它是这样的:

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

What could be wrong?有什么问题?

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.因为 WP7 的文件分隔符是斜杠,而不是反斜杠。

I hope this could help.我希望这会有所帮助。

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.我通常做的是在设计器中加载一个随机图像,然后我从 XAML 代码中复制自动生成的路径并将其用作我自己的.CS代码的模板。

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.如果您 select 选择“始终复制”选项,它也可能会有所帮助。

For resources you the uri format is slightly different that content, what you'll need is something like:对于资源,您的 uri 格式与内容略有不同,您需要的是:

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.第一部分告诉系统在哪个程序集中查找资源。如果您不知道程序集名称,您应该能够在项目属性屏幕中找到它。

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".我不确定,但关键可能是将“资源”更改为“内容”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM