前回はVisual Studioが作成したWPFアプリのDictation_SSpeechにかかわるファイル一式がどのように保存されているかを調べてみました。今回はプロジェクト("WpfApplication1"-Visual Studioの命名です)がどのようなファイルから構成されているかを見てみましょう。
(1)リソース
Resource.Designer.csファイル(後掲)で、Resourceクラスを定義しており、ResourceManagerプロパティでアプリケーションに埋め込んだ"WpfApplication1.Properties.Resources"というリソースファイルにかかわるリソースマネージャー(private static global::System.Resources.ResourceManager resourceMan;)を取得(get)できます。また、Culturerプロパティでリソースのカルチャー(注)(private static global::System.Globalization.CultureInfo resourceCulture;)を取得(get)または設定(set)できます。
注:技術系の方は横棒とマイナス記号の誤読を回避するためにカタカナを横棒無しで記述する伝統があります。(例:カルチャーは「カルチャ」)私は文系だし、読者もそうだと思うので普通に書きますね。
また、xmlファイル(*.xml)とかxmalファイル(*.xaml)とかが出てきますが、これは「XML と XAML の違いは、前者が Extensible Markup Language を指し、後者が Extensible Application Markup Language を指すことです。 一方、前者はマークアップ言語であり、標準一般化マークアップ言語または SGML の非常に複雑なメカニズムを奪われています。後者は XML 自体のサブセットです。XMLドキュメントは、XAML ドキュメントと比較してはるかに柔軟です。」(出典)ということだそうです。
(2)システム設定
Settings.Designer.cs(後掲)というファイルでは、System.Configuration.ApplicationSettingsBaseというクラスから派生させたSettings(internal sealed partial class)というクラスのインスタンスを取得するためのファイルです。実行ファイルの設定にかかわる情報取得のためのもののようです。
(3)実行ファイルのフォーム(ウィンドウ)
MainWindow.xamlというxmlファイル(後掲)があり、これはどうもメインフォームを「高さ350、幅525」に、Grid(注)にScrollViewerを入れ、その中に"textBox"という名のTextBoxを入れるよう記述しているようです。
注:PC環境の違いにかかわらず正確なウィンドウとコントロール配置を行うオブジェクト。(出典)
MainWindow.g.cs(後掲)とMainWindow.g.i.csというファイルがありますが、その内容は見た目、まったく同じです。このファイルはMainWindowクラス(注)の定義の一部で例の"InitializeComponent()”関数を定義しており、その内容はmainwindow.xamlファイルに基づきLoadComponent()関数でコントロールを配置している模様です。また派生元のSystem.Windows.Markup.IComponentConnectorに"Connect(int connectionId, object target)"という関数を定義しており、この関数はTextBoxコントロールにTextBoxクラスをラッピングさせているのではないかと考えます。
注:このクラスはSystem.Windows.WindowとSystem.Windows.Markup.IComponentConnectorから派生させています。なお、MainWindow.xaml.csファイルでもMainWindowクラスの定義を行っていますが、そこでは単位Windowクラスからの派生になっています。("public partial class MainWindow : Window”)
最後にMainWindow.xaml.cs(後掲)というファイルがありますが、これが唯一、機械作成だけでなく、人間(ユーザー)が入力したコードを含むファイルとなっています。
(4)実行ファイル
App.g.cs(後掲)とApp.g.i.csという、またもや同じ内容のファイルがあります。これは(App.xaml.csと同じく)Appクラスの定義ファイルで、"InitializeComponent()”関数でメンバーのStartupUriにSystem.Uri()関数を使って"MainWindow.xaml"のURIを格納するとともに、ここで定番のエントリーポイント定義を行っていました。
public static void Main() {
WpfApplication1.App app = new WpfApplication1.App();
app.InitializeComponent();
app.Run();
}
なお、実行ファイルのプロパティで表示するような属性情報はAssemblyInfo.csで[assembly: Assembly*]を使って設定するようです。
[assembly: AssemblyTitle("WpfApplication1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WpfApplication1")]
[assembly: AssemblyCopyright("Copyright ? ?2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
assembly: ComVisible(false)]
[assembly: ThemeInfo(略)]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
以上です。↑で述べた主要な後掲ファイルは以下を参照してください。なお、これらのコードをビルドしたプログラムがこれです。
次回はこれらの内容を眺めたうえで、「管理系」やXMLのコードを総て除き、手作業のみでMSCompAssにかけるコード書いてみようと考えています。
【Resource.Designer.cs】
//------------------------------------------------------------------------------
// <auto-generated>
// このコードはツールによって生成されました。
// ランタイム バージョン:4.0.30319.17929
//
// このファイルへの変更は、以下の状況下で不正な動作の原因になったり、
// コードが再生成されるときに損失したりします
// </auto-generated>
//------------------------------------------------------------------------------
namespace WpfApplication1.Properties
{
/// <summary>
/// ローカライズされた文字列などを検索するための、厳密に型指定されたリソース クラスです。
/// </summary>
// このクラスは StronglyTypedResourceBuilder クラスによって ResGen
// または Visual Studio のようなツールを使用して自動生成されました。
// メンバーを追加または削除するには、.ResX ファイルを編集して、/str オプションと共に
// ResGen を実行し直すか、または VS プロジェクトをビルドし直します。
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources
{
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources()
{
}
/// <summary>
/// このクラスに使用される、キャッシュされた ResourceManager のインスタンスを返します。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager
{
get
{
if ((resourceMan == null))
{
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("WpfApplication1.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// 厳密に型指定されたこのリソース クラスを使用して、すべての検索リソースに対し、
/// 現在のスレッドの CurrentUICulture プロパティをオーバーライドします。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture
{
get
{
return resourceCulture;
}
set
{
resourceCulture = value;
}
}
}
}
【Settings.Designer.cs】
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.17929
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace WpfApplication1.Properties
{
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
{
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default
{
get
{
return defaultInstance;
}
}
}
}
【MainWindow.xaml】
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ScrollViewer CanContentScroll="True">
<TextBox x:Name="textBox"/>
</ScrollViewer>
</Grid>
</Window>
【MainWindow.g.cs】
#pragma checksum "..\..\MainWindow.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "94A0B46286AACD2319897322C3096C83"
//------------------------------------------------------------------------------
// <auto-generated>
// このコードはツールによって生成されました。
// ランタイム バージョン:4.0.30319.34209
//
// このファイルへの変更は、以下の状況下で不正な動作の原因になったり、
// コードが再生成されるときに損失したりします。
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;
namespace WpfApplication1 {
/// <summary>
/// MainWindow
/// </summary>
public partial class MainWindow : System.Windows.Window, System.Windows.Markup.IComponentConnector {
#line 7 "..\..\MainWindow.xaml"
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
internal System.Windows.Controls.TextBox textBox;
#line default
#line hidden
private bool _contentLoaded;
/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
public void InitializeComponent() {
if (_contentLoaded) {
return;
}
_contentLoaded = true;
System.Uri resourceLocater = new System.Uri("/WpfApplication1;component/mainwindow.xaml", System.UriKind.Relative);
#line 1 "..\..\MainWindow.xaml"
System.Windows.Application.LoadComponent(this, resourceLocater);
#line default
#line hidden
}
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
switch (connectionId)
{
case 1:
this.textBox = ((System.Windows.Controls.TextBox)(target));
return;
}
this._contentLoaded = true;
}
}
}
【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;
using System.Speech.Recognition;
namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
SpeechRecognitionEngine recogEngine;
public MainWindow()
{
InitializeComponent();
recogEngine = new SpeechRecognitionEngine();
// 音声認識が認識処理を終えたときのイベントハンドラを設定する。
recogEngine.SpeechRecognized += recogEngine_SpeechRecognized;
// 音声認識が推定処理を終えたときのイベントハンドラを設定する。
recogEngine.SpeechHypothesized += recogEngine_SpeechHypothesized;
// SystemSpeech を利用したディクテーションを行う場合には、
// DictationGrammar (ディクテーション用の辞書) を追加する。
recogEngine.LoadGrammar(new DictationGrammar());
// 実行環境の標準の入力を音声認識エンジンの入力とする。
recogEngine.SetInputToDefaultAudioDevice();
// 非同期の認識を継続して実行するようにして音声認識を開始する。
recogEngine.RecognizeAsync(RecognizeMode.Multiple);
}
// 推定時の処理
void recogEngine_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
{
this.textBox.Text = "○" + e.Result.Text + "("+ e.Result.Confidence + ")\n" + this.textBox.Text;
}
// 認識時の処理
void recogEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
this.textBox.Text = "●" + e.Result.Text + "(" + e.Result.Confidence + ")\n" + this.textBox.Text;
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
// 終了時には破棄する。
recogEngine.Dispose();
}
}
}
【App.g.cs】
#pragma checksum "..\..\App.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "BA8CBEA9C2EFABD90D53B616FB80A081"
//------------------------------------------------------------------------------
// <auto-generated>
// このコードはツールによって生成されました。
// ランタイム バージョン:4.0.30319.34209
//
// このファイルへの変更は、以下の状況下で不正な動作の原因になったり、
// コードが再生成されるときに損失したりします。
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;
namespace WpfApplication1 {
/// <summary>
/// App
/// </summary>
public partial class App : System.Windows.Application {
/// <summary>
/// InitializeComponent
/// </summary>
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
public void InitializeComponent() {
#line 4 "..\..\App.xaml"
this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative);
#line default
#line hidden
}
/// <summary>
/// Application Entry Point.
/// </summary>
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
public static void Main() {
WpfApplication1.App app = new WpfApplication1.App();
app.InitializeComponent();
app.Run();
}
}
}