WPF で読みがなを取得(1)

WPF なアプリケーションで、IME で入力された文章の読みがなを取得したいことがあります(名前とか住所とかの入力など 😉 )。これは Microsoft から提供されている "Microsoft Visual Studio International Feature Pack 2.0" に含まれている "YomiganaWPFTextBox" コントロールを利用することで実現できますが、このコントロールは MVVM なパターンでは単純に用いることができません。

そこで、コードビハインドで利用するパターンのものと、MVVM で分離するパターンを二回に分けて書いてみます。
(2016年5月16日追記 UWP 用の API をデスクトップアプリケーションから利用する方法を使うことで Windows.Globalization 名前空間の JapanesePhoneticAnalyzer クラスの GetWords メソッドを利用して読み仮名を取得する方法を WPF で読みがなを取得(3) として追加しました)

まずは、"Microsoft Visual Studio International Feature Pack 2.0" の入手が必要です。Microsoft のダウンロード センターから入手することができます(システム要件に .NET Framework 3.5 と書かれていますが、.NET Framework 4.5 でも問題なく利用することができました)。インストールの方法はダウンロード ページに書かれています。

それでは、まずコードビハインドなプログラムです。
動作テストのプログラムは、単純に氏名を入力すると読みがなを表示するものを作っています。

YomiganaTextBox1 の画面
YomiganaTextBox1 の画面

WPF なプロジェクトを作成します(プロジェクト名は YomiganaTextBox1 としました)。

最初に、コントロールへの参照を追加します。Visual Studio のメニューの「プロジェクト」から「参照の追加」を選択します。参照マネージャーが表示されるので、「参照」ボタンをクリックして YomiganaWPFTextBox.dll を選択して「追加」ボタンをクリックします(インストーラーのデフォルトの格納先は "C:\Program Files\Microsoft Visual Studio International Feature Pack 2.0\YomiganaFramework" です)。
YomiganaWPFTextBox の使い方は、インストール フォルダにあるヘルプ ファイル("YomiganaFramework.chm")を参照してください。

次に、XAML ファイルを編集します。

<Window x:Class="YomiganaTextBox1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:yomi="clr-namespace:Microsoft.International.Windows.Controls;assembly=YomiganaWPFTextBox"
        Title="MainWindow" Height="350" Width="525">

    <StackPanel>
        <TextBlock
            Text="Microsoft 製よみがな拡張 TextBox のテスト" FontSize="18" HorizontalAlignment="Center" Margin="8" />

        <StackPanel Orientation="Horizontal" Margin="8" HorizontalAlignment="Center">
            <Label Content="氏名" />
            <yomi:YomiganaWPFTextBox
                Name="yomiTextBox" TextCapturedPreferredConversion="ToHiragana" MinWidth="150"
                YomiganaChanged="yomiTextBox_YomiganaChanged" />
        </StackPanel>

        <StackPanel Orientation="Horizontal" Margin="8" HorizontalAlignment="Center">
            <Label Content="読み" />
            <TextBox Name="Yomigana" MinWidth="150" />
        </StackPanel>
    </StackPanel>
</Window>

次に 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 YomiganaTextBox1
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void yomiTextBox_YomiganaChanged(object sender, RoutedEventArgs e)
        {
            this.Yomigana.Text = this.yomiTextBox.TextCaptured;
        }
    }
}

これで、氏名欄に文字を入力して変換を確定させると、読みがなが表示されるようになります。

見て分かるとおり、YomiganaChanged イベントのハンドラでユーザー入力による変換操作確定時の読みがなを取得して、表示を行うようになっています。また、TextCaptured プロパティは依存関係プロパティではありません。

次回は、MVVM で分離するパターンです。


WPF で読みがなを取得(1)」への1件のフィードバック

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です