2014-03-01 48 views
0

的DefineConstants我有以下標籤的csproj:編輯中的csproj文件

<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宏。

我有.txt文件中,我有以下幾點:

#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 passsing每個宏可我乾脆把它傳遞作爲一個文件或一個字符串,以便它將採取所有這些?這是可能的還是有其他方法?

編輯

這是我的csproj文件:

<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文件;

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

#endif 

回答

1

您可以使用ReadLinesFromFile,該CreatePropertyitem.Replace功能通過使用batching能力MSBuild引擎的更新您的財產:

<!-- 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> 
+0

感謝,所以我必須要提到我所有的#define語句def.txt,並添加上面的代碼,它是正確的? – user2056563

+0

好吧,我會檢查,並回復你 – user2056563

+0

嘿,它不工作,你能告訴我,我做錯了什麼嗎?請看我的編輯 – user2056563