WPF ウィンドウ定義を別のファイルにする

前回はアプリケーション定義内でウィンドウを定義しました。

 *1

ginka.hatenadiary.jp

一般的にウィンドウは別のファイルにします。

今回はウィンドウを別のファイルに作成し、そのウィンドウをプロジェクトファイルに追加したいと思います。

まずはウィンドウ用のファイルを作成します。

ファイル名はMainWindow.xamlにします。

<Window
  xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Title='Hello World'>
</Window>

 

App.xamlから下記の部分を削除します。

<Application
  xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' >
  <Application.MainWindow>
    <Window Title='Hello World' Visibility='Visible' />
  </Application.MainWindow>
</Application>

 

プロジェクトファイルにPageビルド型を追加します。

<Project
  DefaultTargets='Build'
  xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>

  <PropertyGroup>
    <Configuration>Debug</Configuration>
    <Platform>AnyCPU</Platform>
    <RootNamespace>Test</RootNamespace>
    <AssemblyName>Test</AssemblyName>
    <OutputPath>.\bin\Debug\</OutputPath>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include='System' />
    <Reference Include='WindowsBase' />
    <Reference Include='PresentationCore' />
    <Reference Include='PresentationFramework' />
  </ItemGroup>

  <ItemGroup>
    <Page Include='MainWindow.xaml' />
    <ApplicationDefinition Include='app.xaml' />
  </ItemGroup>

  <Import Project='$(MSBuildBinPath)\Microsoft.CSharp.targets' />
  <Import Project='$(MSBuildBinPath)\Microsoft.WinFX.targets' />
</Project>

 

上記のファイルが用意できたらMSBuildでビルドしてください。

 

*1:Application.MainWindow