XAMLとXMLリテラルとLINQ
下記のblogにVisual Basic 9.0のXMLリテラルを使ったおもしろいコードが紹介されていましたので、それを紹介したいと思います。
Visual Basic 9.0では、下記のようにコード内に直接XMLを記述することができ、代入された変数は型推論によりXElementオブジェクトとなります。
Visual Basic
Dim contact1 = _
<contact>
<name>Patrick Hines</name>
<phone type="home">206-555-0144</phone>
<phone type="work">425-555-0145</phone>
</contact>
<contact>
<name>Patrick Hines</name>
<phone type="home">206-555-0144</phone>
<phone type="work">425-555-0145</phone>
</contact>
XamlReader(System.Windows.Markup名前空間)のLoad メソッドを使うと、StreamやXmlReaderからXAMLをロードすることができます。これを使い、下記のようにコード内に記述したXAMLから実際の要素オブジェクトを生成することができます。
Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim XMLliterals = _
<StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>ボタン</Button>
</StackPanel>
Me.Content = System.Windows.Markup.XamlReader.Load(XMLliterals.CreateReader())
End Sub
Dim XMLliterals = _
<StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>ボタン</Button>
</StackPanel>
Me.Content = System.Windows.Markup.XamlReader.Load(XMLliterals.CreateReader())
End Sub
さらにXMLリテラルでは、ASP.NETの埋め込みコードブロックに似た記述方法によりオブジェクトを埋め込むことができます。
Visual Basic
Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim XAMLButton = _
<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>ボタン</Button>
Dim XMLliterals = _
<StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
<%= XAMLButton %>
</StackPanel>
Me.Content = System.Windows.Markup.XamlReader.Load(XMLliterals.CreateReader())
End Sub
Dim XAMLButton = _
<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>ボタン</Button>
Dim XMLliterals = _
<StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
<%= XAMLButton %>
</StackPanel>
Me.Content = System.Windows.Markup.XamlReader.Load(XMLliterals.CreateReader())
End Sub
ということは、当然クエリ式も埋め込めることになり、下記のような記述もできるということになります。
Visual Basic
Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim n As Integer() = {1, 2, 3, 4}
Dim XMLliterals = _
<StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
<%= _
From i _
In n Select _
<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>ボタン その<%= i %></Button> _
%>
</StackPanel>
Me.Content = System.Windows.Markup.XamlReader.Load(XMLliterals.CreateReader())
End Sub
Dim n As Integer() = {1, 2, 3, 4}
Dim XMLliterals = _
<StackPanel xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
<%= _
From i _
In n Select _
<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>ボタン その<%= i %></Button> _
%>
</StackPanel>
Me.Content = System.Windows.Markup.XamlReader.Load(XMLliterals.CreateReader())
End Sub