2017-11-02 14 views
1

私は、動的コンテンツをスプラッシュ画面に追加する方法についてKent Boogaartのexcellent articleに従った。私はスプラッシュ画面上でバージョン番号を見ることができるという意味ではうまくいきます。しかし、この記事では$(Version)というプロパティを使用しています。私の場合は1.0.0.0です。スプラッシュ画面にアセンブリのバージョンを書く

私はshared AssemblyInfo.vbを使用しています。このファイルから次のメジャー/マイナー/ビルド/リビジョン番号を取得してスプラッシュ画面に表示する必要があります。共有AssemblyInfoファイルには、2.5.*のようなバージョン番号が含まれています。私はMSBuild(2.5.abc.xyz)によって生成される実際の番号が必要です。

私はアセンブリからバージョン番号を取得するためにUsingTaskの内側にリフレクションを使用して考えたが、このタスクは前にビルドプロセスを実行しているので、アセンブリはまだ以下に生成されるバージョン番号を持っていませんビルド。さらに、アセンブリは、その時点では存在しない(例えば、クリーンコマンド)。

MSBuildコミュニティタスクをインストールしましたが、SvnVersionタスク以外は表示されません。

生成するバージョン番号をUsingTaskに送信するのに手伝ってもらえますか?

+0

VBプロジェクトのデフォルトの「SplashScreen」を追加します。これには、アプリケーションのリビジョン番号が含まれています。 – kiLLua

+0

@kiLLua:これはWinFormsプロジェクトではありません。 WPF SplashScreenテンプレートは、単にテキストがないPNGファイルです。 – dotNET

+0

実行アセンブリは* next *ビルドアセンブリのバージョンを知っているはずですか?なぜ、現在実行中のアプリケーションのバージョンを表示しないのですか? – mm8

答えて

1

最後に!同じことをしようとする人は、ソリューションディレクトリにある共有AssemblyInfoを使用していることを前提にしています(デフォルトのAssemblyInfoでも動作しますが、パスとファイル名を調整する必要があります):

  1. MSBuild Community Tasksをダウンロードしてインストールしてください。
  2. ターゲットファイル(拡張子が.targetsの単純なXMLファイル)をプロジェクトに追加します。
  3. UsingTaskをKent Boogaartの記事に追加しました。私はこのファイルに質問でリンクしました。これにより、スプラッシュイメージに実際のバージョンの書き込みが実行されます。
  4. 使用<Version><FileUpdate><AddTextToImage>タスク共有AssemblyInfoファイルやスプラッシュイメージに新しいバージョン番号を書くこと(のMSBuildで利用可能な最初の2つがあり、3つ目は、我々はステップ3で追加したUsingTaskからです)。

最終.targetsファイルには、次のようになります。

<?xml version="1.0" encoding="utf-8"?> 
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <UsingTask TaskName="AddTextToImage" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll"> 
    <ParameterGroup> 
     <InputPath ParameterType="System.String" Required="true" /> 
     <OutputPath ParameterType="System.String" Required="true" /> 
     <TopMiddlePoint ParameterType="System.String" Required="true" /> 
     <Text1 ParameterType="System.String" Required="true" /> 
     <Text2 ParameterType="System.String" Required="false" /> 
     <Text3 ParameterType="System.String" Required="false" /> 
    </ParameterGroup> 
    <Task> 
     <Reference Include="WindowsBase" /> 
     <Reference Include="PresentationCore" /> 
     <Reference Include="PresentationFramework" /> 
     <Reference Include="System.Xaml" /> 
     <Using Namespace="System" /> 
     <Using Namespace="System.Globalization" /> 
     <Using Namespace="System.IO" /> 
     <Using Namespace="System.Windows" /> 
     <Using Namespace="System.Windows.Media" /> 
     <Using Namespace="System.Windows.Media.Imaging" /> 
     <Code Type="Fragment" Language="cs"> 
     <![CDATA[   
       var originalImageSource = BitmapFrame.Create(new Uri(InputPath)); 

       var visual = new DrawingVisual(); 

       using (var drawingContext = visual.RenderOpen()) 
       { 
       drawingContext.DrawImage(originalImageSource, new Rect(0, 0, originalImageSource.PixelWidth, originalImageSource.PixelHeight)); 

       var typeFace = new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Bold, FontStretches.Normal); 
       var formattedText = new FormattedText(Text1, System.Globalization.CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeFace, 18, Brushes.Red); 
       var topMiddlePoint = Point.Parse(TopMiddlePoint); 
       var point = new Point(topMiddlePoint.X - (formattedText.Width/2), topMiddlePoint.Y); 
       drawingContext.DrawText(formattedText, point); 

       if(!string.IsNullOrEmpty(Text2)) 
       { 
        formattedText = new FormattedText(Text2, System.Globalization.CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeFace, 18, Brushes.Red); 
        topMiddlePoint.Y += formattedText.Height + 5; 
        point = new Point(topMiddlePoint.X - (formattedText.Width/2), topMiddlePoint.Y); 
        drawingContext.DrawText(formattedText, point); 
       } 

       if(!string.IsNullOrEmpty(Text3)) 
       { 
        formattedText = new FormattedText(Text3, System.Globalization.CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeFace, 18, Brushes.Red); 
        topMiddlePoint.Y += formattedText.Height + 5; 
        point = new Point(topMiddlePoint.X - (formattedText.Width/2), topMiddlePoint.Y); 
        drawingContext.DrawText(formattedText, point); 
       } 

       drawingContext.Close(); 
       } 

       var renderTargetBitmap = new RenderTargetBitmap(originalImageSource.PixelWidth, originalImageSource.PixelHeight, originalImageSource.DpiX, originalImageSource.DpiY, PixelFormats.Pbgra32); 

       renderTargetBitmap.Render(visual); 

       var bitmapFrame = BitmapFrame.Create(renderTargetBitmap); 

       BitmapEncoder encoder = new PngBitmapEncoder(); 

       encoder.Frames.Add(bitmapFrame); 

       using (var stream = File.OpenWrite(OutputPath)) 
       { 
       encoder.Save(stream); 
       stream.Close(); 
       } 
      ]]> 
     </Code> 
    </Task> 
    </UsingTask> 

    <PropertyGroup> 
    <MajorVersion>2</MajorVersion> 
    <MinorVersion>5</MinorVersion> 
    </PropertyGroup> 

    <Target Name="BeforeBuild"> 
    <Version BuildType="Automatic" RevisionType="Automatic" Major="$(MajorVersion)" Minor="$(MinorVersion)"> 
     <Output TaskParameter="Major" PropertyName="Major" /> 
     <Output TaskParameter="Minor" PropertyName="Minor" /> 
     <Output TaskParameter="Build" PropertyName="Build" /> 
     <Output TaskParameter="Revision" PropertyName="Revision" /> 
    </Version> 

    <FileUpdate Files="$(SolutionDir)SharedAssemblyInfo.vb" 
      Regex="Assembly: AssemblyVersion\(&quot;\d+\.\d+((\.\*)|(\.\d+\.\d+))?&quot;\)" 
      ReplacementText="Assembly: AssemblyVersion(&quot;$(Major).$(Minor).$(Build).$(Revision)&quot;)" /> 

    <FileUpdate Files="$(SolutionDir)SharedAssemblyInfo.vb" 
      Regex="Assembly: AssemblyFileVersion\(&quot;\d+\.\d+((\.\*)|(\.\d+\.\d+))?&quot;\)" 
      ReplacementText="Assembly: AssemblyFileVersion(&quot;$(Major).$(Minor).$(Build).$(Revision)&quot;)" /> 

    <SvnVersion LocalPath="."> 
     <Output TaskParameter="Revision" PropertyName="SvnRevision" /> 
    </SvnVersion> 

    <AddTextToImage InputPath="$(ProjectDir)Resources\Splash.png" 
        OutputPath="$(ProjectDir)Resources\SplashWithVersion.png" 
        TopMiddlePoint="250,150" 
        Text1="$(Major).$(Minor).$(Build).$(Revision)" 
        Text2="SVN Version: $(SvnRevision)" /> 

    <Message Text="Updated version number on splash screen to: $(Major).$(Minor).$(Build).$(Revision)" Importance="high"/> 
    </Target> 
</Project> 

これはあなたのAssemblyInfoと出力イメージを更新します。 Visual Studioでは、出力イメージをSplashScreen(ビルドアクションとして)とマークする必要があります。また、スプラッシュ画面にはアセンブリバージョンとSVNリビジョン番号の両方を書いています。必要に応じて調整することができます。

関連する問題