为什么修改项目输出目录会导致:IOException未处理“无法找到资源'app.xaml'。”

时间:2013-06-13 09:15:12

标签: c# wpf xaml visual-studio-2012 msbuild

为了将项目设置合并到C ++和C#项目的属性表中,构建了以下属性表:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!--
      Trying to support both C++ and C# projects by introducing derived 
      properties and setting the appropriate output properties.
  -->
  <PropertyGroup Label="UserMacros">
    <ProjectOrAssemblyName Condition="'$(AssemblyName)'==''">$(ProjectName)</ProjectOrAssemblyName>
    <ProjectOrAssemblyName Condition="'$(ProjectName)'==''">$(AssemblyName)</ProjectOrAssemblyName>
    <ShortPlatform Condition="'$(Platform)'=='Win32'">x86</ShortPlatform>
    <ShortPlatform Condition="'$(Platform)'=='x86'">x86</ShortPlatform>
    <ShortPlatform Condition="'$(Platform)'=='x64'">x64</ShortPlatform>
    <ShortPlatform Condition="'$(Platform)'=='AnyCPU'">AnyCPU</ShortPlatform>
  </PropertyGroup>
  <PropertyGroup>
    <OutputPath>$(OutputRelativePath)/$(ProjectOrAssemblyName)_$(ShortPlatform)_$(Configuration)/</OutputPath>        
    <BaseIntermediateOutputPath>$(OutputRelativePath)/Obj_Exe/$(ProjectOrAssemblyName)_$(ShortPlatform)</BaseIntermediateOutputPath>
    <IntermediateOutputPath>$(BaseIntermediateOutputPath)_$(Configuration)/</IntermediateOutputPath>
    <IntDir>$(IntermediateOutputPath)</IntDir>
    <OutDir>$(OutputPath)</OutDir>
  </PropertyGroup>
</Project>

此属性表将所有构建输出移动到单独的位置 OutputRelativePath (在单独的属性表中定义,或直接在项目文件中),包含源代码的目录外部,以便于清理等。但是,在设置之后这个和构建工作正常,并且所有单元测试工作正常,很明显WPF可执行项目不是很好,因为使用上面的属性表运行应用程序导致臭名昭着:

IOException was unhandled "Cannot locate resource 'app.xaml'."

为什么更改输出路径会导致此错误?如何确定原因是项目构建输出路径?这可以在生成的代码中看到吗?我找不到?这不是一个错误吗?

注意:使用以下属性表有效,但仅当 IntermediateOutputPath 包含 BaseIntermediateOutputPath

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <OutputPath>$(OutputRelativePath)/$(AssemblyName)_$(Platform)_$(Configuration)</OutputPath>
    <BaseIntermediateOutputPath>$(OutputRelativePath)/Obj_Exe/$(AssemblyName)_$(Platform)</BaseIntermediateOutputPath>
    <IntermediateOutputPath>$(BaseIntermediateOutputPath)_$(Configuration)</IntermediateOutputPath>
  </PropertyGroup>
</Project>

所以看来,不知何故,输出路径包含AssemblyName属性或类似属性。

另一个组装中的XAML样式的更新:如果这些 - 例如xaml ResourceDictionary - 同样适用于Brushes.xaml - 位于另一个程序集中,此程序集也更改了OutputPath,这也引发了异常:

XamlParseException was unhandled for set property Source 
with InnerException "Cannot locate resource 'Brushes.xaml'" 

总而言之,输出位置会改变xaml资源名称,因此无法以某种方式在运行时发现这些名称。奇怪的是它在设计时不是问题......


更新:重现异常的最小步骤:

打开Visual Studio 2013

创建新的C#项目WPF应用程序,例如XamlIntermediateOutputPathBug

卸载项目

编辑项目文件

在第一个PropertyGroup之后插入新的PropertyGroup:

<PropertyGroup>
  <OutputRelativePath>$(ProjectDir)..\Build</OutputRelativePath>
  <OutputPath>$(OutputRelativePath)/$(AssemblyName)_$(Platform)_$(Configuration)/</OutputPath>
  <BaseIntermediateOutputPath>$(OutputRelativePath)/Obj_Exe/$(AssemblyName)_$(Platform)</BaseIntermediateOutputPath>
  <IntermediateOutputPath>$(BaseIntermediateOutputPath)_$(Configuration)/</IntermediateOutputPath>
  <IntDir>$(IntermediateOutputPath)</IntDir>
  <OutDir>$(OutputPath)</OutDir>
</PropertyGroup>  

删除其余PropertyGroup中的OutputPath个属性,例如

<OutputPath>bin\Debug\</OutputPath>  

和:

<OutputPath>bin\Release\</OutputPath>  

然后,这应该为IOException开始mainwindow.xaml。这是由于$(AssemblyName).g.resources嵌入式资源的名称如下:

.mresource public 'Build/Obj_Exe/XamlIntermediateOutputPathBug_AnyCPU_Debug/XamlIntermediateOutputPathBug.g.resources' as Build_Obj_Exe_XamlIntermediateOutputPathBug_AnyCPU_Debug_XamlIntermediateOutputPathBug.g.resources
{
  // Offset: 0x00000000 Length: 0x000003BC
}
.mresource public 'Build/Obj_Exe/XamlIntermediateOutputPathBug_AnyCPU_Debug/XamlIntermediateOutputPathBug.Properties.Resources.resources' as Build_Obj_Exe_XamlIntermediateOutputPathBug_AnyCPU_Debug_XamlIntermediateOutputPathBug.Properties.Resources.resources
{
  // Offset: 0x000003C0 Length: 0x000000B4
}

可以在ildasm.exe中看到,并为程序集打开MANIFEST。还可以看出,正常资源也会以输出路径为前缀获取错误的名称。但是,可以通过在此资源的项目文件中设置LogicalName来修复此问题(请参阅MissingManifestResourceException when running tests after building with MSBuild (.mresource has path in manifest))。对于xaml资源来说,这似乎不可能......

看了我注意到的配置后,我在/OutputPath的末尾使用了IntermediateOutputPath,删除它们似乎有效,见下文:

<PropertyGroup>
  <OutputRelativePath>$(ProjectDir)..\Build</OutputRelativePath>
  <OutputPath>$(OutputRelativePath)/$(AssemblyName)_$(Platform)_$(Configuration)</OutputPath>
  <BaseIntermediateOutputPath>$(OutputRelativePath)/Obj_Exe/$(AssemblyName)_$(Platform)</BaseIntermediateOutputPath>
  <IntermediateOutputPath>$(BaseIntermediateOutputPath)_$(Configuration)</IntermediateOutputPath>
  <IntDir>$(IntermediateOutputPath)/</IntDir>
  <OutDir>$(OutputPath)/</OutDir>
</PropertyGroup>  

我觉得这很奇怪......任何洞察为什么会是这种情况或者如果这是真的,我们将不胜感激。请注意,C ++ IntDirOutDir必须具有反斜杠,否则您将收到有关此内容的警告。


2 个答案:

答案 0 :(得分:4)

将MSBuild输出详细程度设置为“Diagnostic”可以快速显示问题的根源:

1>   (TaskId:21)
1>  Microsoft (R) Build Task 'ResourcesGenerator' Version '4.0.30319.33440 built by: FX45W81RTMREL'. (TaskId:21)
1>  Copyright (C) Microsoft Corporation 2005. All rights reserved. (TaskId:21)
1>  
1>   (TaskId:21)
1>  Generating .resources file: '..\Build/Obj_Exe/WpfApplication8_AnyCPU_Debug/WpfApplication8.g.resources'... (TaskId:21)
1>  Reading Resource file: 'C:\Users\hpass_000\Projects\Build\Obj_Exe\WpfApplication8_AnyCPU_Debug\MainWindow.baml'... (TaskId:21)
1>  Resource ID is 'mainwindow.baml'. (TaskId:21)
1>  Generated .resources file: '..\Build/Obj_Exe/WpfApplication8_AnyCPU_Debug/WpfApplication8.g.resources'. 

注意路径名中的前向和后向斜杠的混合。 Windows本身知道如何很好地处理路径名中的正斜杠。但是在其他软件中通常缺乏这种能力,缺乏资源生成器任务。这需要一个真正的反斜杠作为路径分隔符,正斜杠在资源名称中有效。修正:

 <OutputPath>$(OutputRelativePath)\$(AssemblyName)_$(Platform)_$(Configuration)\</OutputPath>
 <BaseIntermediateOutputPath>$(OutputRelativePath)\Obj_Exe\$(AssemblyName)_$(Platform)</BaseIntermediateOutputPath>
 <IntermediateOutputPath>$(BaseIntermediateOutputPath)_$(Configuration)\</IntermediateOutputPath>

换句话说,我只是将/替换为\。这解决了这个问题。

答案 1 :(得分:1)

当Xaml引用位于当前项目中的类型时,WinFX和Xaml目标会执行一些幕后黑客/魔法。在此构建任务期间,将wpf.csproj复制到tempfilename.tmp_proj,修剪与程序集引用相关的几个节点,并将该文件编译到IntermediateOutputPath中。这允许Xaml编译器引用临时程序集中的类型。