编辑csproj文件中的DefineConstants [英] Edit the DefineConstants in csproj file

查看:134
本文介绍了编辑csproj文件中的DefineConstants的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有以下标签的csproj:

I have csproj with the following tag:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>Bin\Debug</OutputPath>
    <DefineConstants>TRACE;DEBUG;SILVERLIGHT;WINDOWS_PHONE;SUPPORTS_ICLOUD</DefineConstants>
    <NoStdLib>true</NoStdLib>
    <NoConfig>true</NoConfig>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

在这里,如果您看到我添加了一个名为 SUPPORTS_ICLOUD

Here if you see that i have added a macro called SUPPORTS_ICLOUD.

我有.txt文件,因为我有以下内容:

I have .txt file in that i have the following :

#define SUPPORTS_IN_APP_PURCHASE 

#define BUTTON_THEME    
#define SUPPORTS_TITLE 

#define HAS_HTML_IMAGE_CONTENT 
#define TINT_COLOR 
#define NAVIGATIONTITLE_TEXT_COLOR 

#define SUPPORTS_ICLOUD 

如您在我的文本文件中所见我定义了很多宏,现在可以将其作为文件或字符串一起传递,而不是通过 DefineConstants 中的每个宏进行传递,以便将所有这些传递给我?这是可能的还是还有其他方法?

As you can see in my text file i have many macros defined, now instead of passsing each macro in DefineConstants can i pass it altogether as a file or a string so that it will take all those ? Is this possible or is there any other way ?

编辑

这是我的csproj文件:

This is my csproj file:

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>10.0.20506</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{0D446418-B7CD-4624-91F4-F3E382F8DD23}</ProjectGuid>
    <ProjectTypeGuids>{C089C8C0-30E0-4E22-80C0-CE093F111A43};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>POCChild3</RootNamespace>
    <AssemblyName>POCChild3</AssemblyName>
    <TargetFrameworkIdentifier>WindowsPhone</TargetFrameworkIdentifier>
    <TargetFrameworkVersion>v8.0</TargetFrameworkVersion>
    <SilverlightVersion>$(TargetFrameworkVersion)</SilverlightVersion>
    <SilverlightApplication>true</SilverlightApplication>
    <SupportedCultures>
    </SupportedCultures>
    <XapOutputs>true</XapOutputs>
    <GenerateSilverlightManifest>true</GenerateSilverlightManifest>
    <XapFilename>POCChild3_$(Configuration)_$(Platform).xap</XapFilename>
    <SilverlightManifestTemplate>Properties\AppManifest.xml</SilverlightManifestTemplate>
    <SilverlightAppEntry>POCChild3.App</SilverlightAppEntry>
    <ValidateXaml>true</ValidateXaml>
    <MinimumVisualStudioVersion>11.0</MinimumVisualStudioVersion>
    <ThrowErrorsInValidation>true</ThrowErrorsInValidation>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>Bin\Release\</OutputPath>
    <DefineConstants>DEBUG;TRACE;SILVERLIGHT;WINDOWS_PHONE</DefineConstants>
    <NoStdLib>true</NoStdLib>
    <NoConfig>true</NoConfig>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>Bin\Release</OutputPath>
    <DefineConstants>TRACE;SILVERLIGHT;WINDOWS_PHONE</DefineConstants>
    <NoStdLib>true</NoStdLib>
    <NoConfig>true</NoConfig>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup>
    <MacroFile>Macros.txt</MacroFile>
  </PropertyGroup>
  <Target Name="Build">
    <ReadLinesFromFile
         File="$(MacroFile)" >
      <Output
          TaskParameter="Lines"
          ItemName="MacrosFromFile"/>
    </ReadLinesFromFile>

    <CreateProperty
        Value="@(MacrosFromFile->Replace('#define ', ''))">
      <Output
          TaskParameter="Value"
          PropertyName="FileDefineConstants" />
    </CreateProperty>

    <CreateProperty
        Value="$(DefineConstants);$(FileDefineConstants)">
      <Output
          TaskParameter="Value"
          PropertyName="DefineConstants" />
    </CreateProperty>

    <Message Text="Const >> $(DefineConstants)" />
  </Target>

在我的MainPage.cs文件中;

In my MainPage.cs file;

#if SUPPORTS_ICLOUD 
            AppName.Text = "ABCD";
            Debug.WriteLine("App type is app with main screen");

#endif


推荐答案

您可以使用 ReadLinesFromFile CreateProperty item.Replace 函数通过使用 batching msbuild引擎的功能:

You can use ReadLinesFromFile, the CreateProperty and the item.Replace function to update your property by using the batching capabilities of the msbuild engine:

  <!-- property defintion -->
  <PropertyGroup>
     <MacroFile>def.txt</MacroFile> <!-- the filename with your #defines -->
  </PropertyGroup>

  <!-- other stuff in your build file -->

  <!-- import common targets near the end of your build file -->
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

  <!-- override targets (make sure this is AFTER the import elements) -->
  <!-- BeforeBuild is pre-dfined target which can be overriden -->
  <Target Name="BeforeBuild">
       <!-- Open the #define file and read every line in items named
            MacrosFromFile 
        -->
       <ReadLinesFromFile
            File="$(MacroFile)" >
            <Output
                TaskParameter="Lines"
                ItemName="MacrosFromFile"/>
        </ReadLinesFromFile>

        <!-- Create a new property called FileDefineConstants combining 
             every Item from MacrosFromFile   
             using the built-in replace statement to get 
             rid of the #define instruction
         -->
        <CreateProperty
            Value="@(MacrosFromFile->Replace('#define ', ''))">
            <Output
                TaskParameter="Value"
                PropertyName="FileDefineConstants" />
        </CreateProperty>

        <!-- re-create the orignal DefineConstants combining the current value
             and the value from FileDefineConstants -->
        <CreateProperty
            Value="$(DefineConstants);$(FileDefineConstants)">
            <Output
                TaskParameter="Value"
                PropertyName="DefineConstants" />
        </CreateProperty>

        <Message Text="Const >> $(DefineConstants)" />
  </Target>

这篇关于编辑csproj文件中的DefineConstants的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆