WPF 4 をコードで使う方法は前回で解ったので今回は XAML を。
System.Xaml が新たに作られたということは xmlns 名前空間が変わる?
というか System.Windows.Markup から書き換えも必要なのだろうか?
WPF から System.Xaml に移行した型
難しく説明しないでくれよ…
WPF の XAML 名前空間は以前と同じでイイということは解ったけど本当かな。
[XAML] WPF 4 を使用して、Windows 7 のジャンプ リストにカスタム タスクを追加する | 逆引きサンプル コード
上記とかでも 3.5 と同じだしイケそうだ。
後は IronPython で自分で書いた下記のようにソースコードに埋め込みしたいのだが。
IronPython で XAML
C# では Python の docstring みたく丸ごと XML とはいかない。
@”リテラル” で改行付き文字列はイケるけど ” を “” にする必要あり。
string (C#)
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
class TextReader : Window
{
private const string TXT = @"
<TextBlock
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
Hello World
</TextBlock>";
public TextReader()
{
this.Title = "XamlReader.Parse Test";
this.Width = 400;
this.Height = 400;
try
{
var tt = XamlReader.Parse(TXT) as TextBlock;
this.Content = tt;
tt.Text = "こんにちは日本";
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
class __main__
{
[STAThread]
public static void Main(string[] args)
{
var app = new Application();
app.Run(new TextReader());
}
}
これをビルドしても上手くいったけど XAML は通常 ” が多いし…
C# では別ファイルにして XamlReader.Load() のほうがいいな。
それと XAML から object を作ったら as で型を教えてあげないとメソッドが使えない。
IronPython は動的だからいらないけどコンパイルする C# では必須だね。
WPF 4.0 Text Stack Improvements – WPF Text Blog – Site Home – MSDN Blogs
とりあえずこの Text Rendering API ってのを試してみよう。
3.5 までは Windows XP での描写が変だったのがどうなるかだ。
私的にはもう XP はガン無視でいいけど、いつの OS だよ Linux 使えよ。
でも世間はそうじゃないし、しかたがないので XP に .NET 4.0 を入れる。
Windows Update に Client Profile ってのがあるんだね。

意外とサイズが小さいな、これなら仮想 OS な XP に入れてもいいや。
入れてみたら csc.exe なんかもしっかりあるのでコンパイルできると思う。

と思ったけど何このエラーは…

build した exe を持っていくと問題なく動くんだが、よくワカンネェ。
本当に XP って糞 OS だ、俺は XP で開発しないから無視しておこう。
てか Ubuntu 上の仮想マシンなので PC を行ったり来たりでメンドイ。
スクリーンショットも全部 Ubuntu から撮影してるし、関係無いか。
というわけでコード。
test.xaml
<StackPanel
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
TextOptions.TextFormattingMode="Display">
<TextBlock Name="block">
クリアータイプ ... ClearType
</TextBlock>
<TextBlock TextOptions.TextRenderingMode="Grayscale">
白黒 ... Grayscale
</TextBlock>
<TextBlock TextOptions.TextRenderingMode="Aliased">
ぎざぎざ ... Aliased
</TextBlock>
</StackPanel>
src.cs
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
class TextRender : Window
{
public TextRender()
{
this.Title = "てきすとれんだぁ!";
this.Width = 300;
this.Height = 200;
try
{
var fs = new FileStream("test.xaml", FileMode.Open, FileAccess.Read);
var sp = XamlReader.Load(fs) as StackPanel;
this.Content = sp;
(sp.FindName("block") as TextBlock).MouseUp += on_click;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
private void on_click(object sender, RoutedEventArgs e)
{
MessageBox.Show("くりっく");
}
}
class __main__
{
[STAThread]
public static void Main(string[] args)
{
var app = new Application();
app.Run(new TextRender());
}
}
まとめて ZIP
textrender.zip
描写だけじゃ寂しいので一番上の文字列をクリックしたらメッセージを出すように。
ココも as で型指定が必要だ、やはり XAML をコンパイルするのとは扱いが違うわな。


うん、XP でも綺麗なフォント表示が可能になっている。
XP だと個々の差は解りにくいけど 7 でははっきり違いが解る。
しかし画像を並べてみると本当に XP の UI って古臭いなぁ…
今日はこのへんで。
追記
ビルドするためのバッチに
build32.bat
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe ^
/lib:C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF ^
/reference:System.dll ^
/reference:System.Xml.dll ^
/reference:System.Xaml.dll ^
/reference:PresentationCore.dll ^
/reference:PresentationFramework.dll ^
/reference:WindowsBase.dll ^
/target:winexe ^
/out:TextReader.exe ^
/win32icon:icon.ico ^
/optimize+ ^
src.cs
PAUSE
と System.dll と System.Xml.dll への参照を追記したら XP でもビルドできた。
何で 7 と違うのよ…