XAMLとXML
XAMLは、ソフトウェアの表示画面などを記述するためのXMLベースのマークアップ言語なので、基本的にXMLを理解すれば、XAMLは理解できる。
MainWindow.xaml
Visual StudioでWPFアプリケーションのプロジェクトを新規作成すると、MainWindow.xamlとMainWindow.xaml.csは以下のようになっている。(Titleの内容は若干変えている)
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CashRegisterApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
MainWindow.xaml
<Window x:Class="CashRegisterApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CashRegisterApplication"
mc:Ignorable="d"
Title="MainWindow" Height="700" Width="800">
<Grid>
</Grid>
</Window>
xamlの方の説明をしていく。
1行目の「x:Class」は、そのxamlコードの表すクラス名を表し、C#コードの部分クラスと対応させている。
そのためC#のコードのクラスにはpartialという記載があるので、ここで部分クラスであるMainWindowと対応しているのがわかる。
2~6行目は名前空間を定義している。
名前空間に関してはxmlの以下の説明がわかりやすかった。
(https://www.kanzaki.com/docs/sw/names.html)
7行目は、接頭辞”d”をアプリ実行時に無視するような設定をしている。
Grid
MainWindow.xamlの9行目のGridは、行と列を定義して画面レイアウトを構成する。
参考サイト:
https://anderson02.com/cs/wpf/wpf-3/
参考サイト