读取VS2012中任何WPF项目的资源文件时出错
本文关键字:资源 源文件 出错 项目 VS2012 任何 WPF 读取 | 更新日期: 2023-09-27 18:23:53
每当我试图在VS2012中编译任何WPF项目时,我都会遇到以下编译错误:
读取资源文件"c:''Users''myuserID''Documents''Visual Studio"时出错2012''Projects''MyAppNAme''MyAppNAme''obj''Debug'''-'系统无法找到指定的路径。'C: ''Users''myuserID''Documents''Visual Studio2012''Projects''MyAppNAme''MyAppNAme''CSC MyAppNAme
问题是,当VS生成CSC命令时,它包含一个额外的部分重复/资源参数,如下所示:
/resource:obj'Debug' /resource:obj'Debug'MyAppName.Properties.Resources.resources
CSC只查看第一个参数,由于没有指定文件名,因此会抛出错误。我进入命令行模式,在有和没有额外参数的情况下进行了测试,并验证了这是问题所在。我不想手动编译我的项目!
有人知道VS是如何得出这些参数的吗?我如何才能去掉这个额外的无效参数?
构建输出看起来像这样(Framework 4.5)
1>Target "MainResourcesGeneration" in file "C:'Windows'Microsoft.NET'Framework'v4.0.30319'Microsoft.WinFX.targets" from project "C:'Users'jws15592'Documents'Visual Studio 2012'Projects'MyAppName'MyAppName'MyAppName.csproj" (target "PrepareResources" depends on it):
1>Building target "MainResourcesGeneration" completely.
1>Input file "C:'Users'jws15592'Documents'Visual Studio 2012'Projects'MyAppName'MyAppName'obj'Debug'MainWindow.baml" is newer than output file "obj'Debug'".
1>Task "Message" skipped, due to false condition; ('$(MSBuildTargetsVerbose)'=='true') was evaluated as (''=='true').
1>Using "ResourcesGenerator" task from assembly "PresentationBuildTasks, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35".
1>Task "ResourcesGenerator"
1>
1>
1> Microsoft (R) Build Task 'ResourcesGenerator' Version '4.0.30319.17929 built by: FX45RTMREL'.
1> Copyright (C) Microsoft Corporation 2005. All rights reserved.
1>
1>
1> Generating .resources file: 'obj'Debug''...
1>C:'Windows'Microsoft.NET'Framework'v4.0.30319'Microsoft.WinFX.targets(709,5): error RG1000: Unknown build error, 'Could not find a part of the path 'C:'Users'jws15592'Documents'Visual Studio 2012'Projects'MyAppName'MyAppName'obj'Debug''.'
1>Done executing task "ResourcesGenerator" -- FAILED.
1>Done building target "MainResourcesGeneration" in project "MyAppName.csproj" -- FAILED.
项目文件中的项目组:
<ItemGroup>
<Compile Include="Properties'AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Properties'Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Properties'Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<EmbeddedResource Include="Properties'Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="app.config" />
<None Include="Properties'Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<AppDesigner Include="Properties'" />
</ItemGroup>
这不是一个额外的参数,它是一个不完整的参数。您通常会在那里看到/resource:obj''Debug''MyAppName.g.resources。某些东西使文件名的计算结果为空字符串。它是从应用程序中的.xml文件生成的。
Tools+Options,Projects and Solution,Build and Run,"MSBuild project Build output verbose"设置,将其更改为Detailed。现在,您将获得构建步骤的非常详细的跟踪。将其复制/粘贴到记事本中,然后打开"格式+换行",这样您就可以看到所有内容。
生成MyAppName.g.resources文件的预期构建步骤如下所示:
1>Output file "obj'Debug'MyAppName.g.resources" does not exist.
1>Task "Message" skipped, due to false condition; ('$(MSBuildTargetsVerbose)'=='true') was evaluated as (''=='true').
1>Using "ResourcesGenerator" task from assembly "PresentationBuildTasks, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35".
1>Task "ResourcesGenerator"
1>
1>
1> Microsoft (R) Build Task 'ResourcesGenerator' Version '4.0.30319.17929 built by: FX45RTMREL'.
1> Copyright (C) Microsoft Corporation 2005. All rights reserved.
1>
1>
1> Generating .resources file: 'obj'Debug'MyAppName.g.resources'...
1> Reading Resource file: 'C:'Users'hpass_000'AppData'Local'Temporary Projects'MyAppName'obj'Debug'MainWindow.baml'...
1> Resource ID is 'mainwindow.baml'.
1> Generated .resources file: 'obj'Debug'MyAppName.g.resources'.
将其与您的进行比较,并告诉我们您所看到的不同之处。
更新:很明显,这个构建步骤出错的原因完全相同。输入文件已正确生成。我可以将其缩小为一个MSBuild变量,它有一个空字符串,它是$(_ResourceNameInMainAssembly)
。
然而,解释为什么要困难得多,我看不出任何情况下它可能是空的。使用文本编辑器查看C:''Windows''Microsoft.NET''Framework''v4.0.30319''Microsoft.WinFx.targets。文件中分配变量的部分如下所示:
<_ResourceNameInMainAssembly Condition="'$(UICulture)' == ''">$(AssemblyName).g.resources</_ResourceNameInMainAssembly>
<_ResourceNameInMainAssembly Condition="'$(UICulture)' != ''">$(AssemblyName).unlocalizable.g.resources</_ResourceNameInMainAssembly>
该.targets文件长42567字节,在我的机器上的日期为2012年6月6日。我确实安装了.NET 4.5。
作为一种解决方法,您可以将实际资源文件符号链接到"丢失"的resources.resources
文件。我遇到了同样的问题,只需在提升的命令提示符中键入以下内容即可解决。
mklink c:'MySolution'MyLibrary'MyLibrary'Properties'Resources.resources c:'MySolution'MyLibrary'MyLibrary'Properties'Resources.Designer.cs
这是个奇怪的数字。
我不知道是否有任何设置可以直接从IDE中篡改,这可能会影响这种奇怪的行为。在检查了命令行参数之后,就像您所做的那样,我会把手弄脏,并在一些文本编辑器(例如记事本)中打开项目文件。在那里,我将检查所有具有<EmbeddedResource>
或<Content Include="Something">
作为子元素的<ItemGroup>
节点,直到找到罪魁祸首。您应该检查的其他文件是Properties文件夹中的*.resx文件。
希望能有所帮助。