代码之家  ›  专栏  ›  技术社区  ›  Todd Main

msbuild:如何在Silverlight生成中同时包含“*.xaml”和“*.cs”?

  •  5
  • Todd Main  · 技术社区  · 14 年前

    我想使用msbuild为2个文件获取并创建相关元素。如果只是一个文件扩展名,我将使用:

    <ItemGroup>
        <Compile Include="\Pages\*.cs" /> 
    </ItemGroup>
    

    在用于Silverlight生成的.csproj文件中,每个用户控件的设置方式与它自己的设置方式相同 <Compile> 元素和子元素 <DependentUpon> 元素:

    <ItemGroup>
        <Compile Include="Pages\SilverlightControl1.xaml.cs">
            <DependentUpon>SilverlightControl1.xaml</DependentUpon>
        </Compile>
        <Compile Include="Pages\SilverlightControl2.xaml.cs">
            <DependentUpon>SilverlightControl2.xaml</DependentUpon>
        </Compile>
    </ItemGroup>
    

    在msbuild文件中,我要指定:

    抓住所有 .cs 文件夹 把它们放进 Include 属性并获取相同的文件名-减去 把它放进 <依赖于> 元素。

    因此,匹配文件对就像(伪)一样:

    <ItemGroup>
        <Compile Include="Pages\*.cs">
            <DependentUpon>Pages\*.xaml</DependentUpon>
        </Compile>
    </ItemGroup>
    

    有没有办法把上面的内容放到msbuild中?

    2 回复  |  直到 14 年前
        1
  •  4
  •   Ruben Bartelink    14 年前

    msbuild有两个单独的元数据属性,称为 %(Filename) (不带扩展名的文件名)和 %(Extension) 在您的示例中是“.cs”。所以,我想知道这是否可能:

    <ItemGroup>
        <Compile Include="Pages\*.cs">
            <DependentUpon>%(Directory)%(Filename)</DependentUpon>
        </Compile>
    </ItemGroup>
    

    但是,我不认为你会喜欢它会做什么,甚至不想做你想做的。

    您真的应该只有“glob”类型的项目(*.cs) 在内部 目标-不应将其声明为顶级项组,否则它将在Visual Studio中执行有趣的操作,并且(例如)将所有.cs文件添加到版本控制中,甚至可能将*.cs扩展到项目中的单个项中。

    以下是我在非Visual Studio MSBuild项目中的建议:

    <Target Name="PrepareCompileItems">
        <XamlFiles Include="Pages\*.cs">
            <DependentUpon>%(Directory)%(Filename)</DependentUpon>
        </XamlFiles>
    
        <Compile Include="@(XamlFiles)" />
    </Target>
    

    如果你是在一个vs项目中这样做的话,那就更难了——因为你想 添加 元数据到已存在的项组,以在编译之前强制DependentOn关联:

    <Target Name="AddDependentUponMetadata">
        <CsFiles Include="Pages\*.cs" />
    
        <XamlFiles Include="@(CsFiles)">
            <DependentUpon>%(Directory)%(Filename)</DependentUpon>
        </XamlFiles>
    
        <Compile Remove="@(CsFiles)" />    
        <Compile Include="@(XamlFiles)" />
    </Target>
    

    不过,我在输入这段代码时并没有实际测试我的断言,所以Ymmv…

        2
  •  2
  •   Malcolm    14 年前

    在msbuild中,可以执行以下操作:

    <ItemGroup>
            <ClassFiles Include="**\*.cs"/>
            <XamlFiles Include="**\*.xaml"/>
            <Compile  Include="@(ClassFiles)" > 
                <DependentUpon>"@(XamlFiles)"</DependentUpon>
            </Compile>
         </ItemGroup>
    

    这是你想要的还是我远离你的问题?