program study story

プログラムの勉強 アウトプット

XAML_簡単構成について_基礎知識

XAMLWindowsアプリケーションを作成するには

少なくとも2つの.xamlファイル(=拡張子が「.xaml」のファイル)が必要。

・ウィンドウを定義した.xamlファイル

・アプリケーションを定義した.xamlファイル

 

例1

・MainWindow.xaml(ウィンドウを定義した.xamlファイル)

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="MainWindow" Height="450" Width="800">
Hello World
</Window>

・app.xaml(アプリケーションを定義した.xamlファイル)

<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
StartupUri="win.xaml"
/>

XAMLでは、プロジェクト・ファイル(.projファイル)を記述し、ビルド・ツール (MSBuild.exe)を使って実行ファイルを作成する。

--------------------------------------------------------------------------------------------------

ウィンドウ定義(MainWindow.xaml)とアプリケーション定義(app.xaml)とプロジェクト・ファイル(hello.proj)

ビルド・ツール(MSBuild.exe)

実行ファイル(HelloWorld.exe)

--------------------------------------------------------------------------------------------------

というexeファイル生成の流れ。

 

XML名前空間の基礎知識

XMLは、各要素が属する名前空間をxmlsという特別な属性を使って表す。

例2 --------------------------

<タグ1>
    xmlns="MyNameSpace"
    属性1="……" 属性2="……" >
  <タグ2>
    ……
  </タグ2>
</タグ1>

--------------------------------

例2においては、<タグ1>要素と、その子要素である<タグ2>要素はMyNameSpace名前空間に属していることを表している。

 

例1のMainWindow.xamlでは、

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

……

という名前空間に属していることを指示しています。

 

■ プロジェクト・ファイルの記述

(例) _プロジェクト・ファイル

--------------------------------------------------------------------------------------------------

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

<PropertyGroup>
<AssemblyName>HelloWorld</AssemblyName>
<OutputType>MainWindowexe</OutputType>
<OutputPath>.\</OutputPath>
</PropertyGroup>

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

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

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

</Project>

--------------------------------------------------------------------------------------------------

<AssemblyName>HelloWorld</AssemblyName>

 ↑のタグは、.exeファイルに使用される名前。

<ApplicationDefinition Include="app.xaml" />

 ↑のタグは、アプリケーションを定義した.xamlファイル。

<Page Include="MainWindow.xaml" />

 ↑のタグは、アプリケーション定義が必要とする.xamlファイル。