Last reviewed: 7/27/2024 8:21:12 AM

.NET WPF Applications

Develop .NET WPF applications that manage grammars using your favorite .NET WPF development tools. This includes development environments such as Microsoft Visual C# .NET and Microsoft Visual Basic .NET.

The following sections describe the steps for integrating GrammarKit with .NET WPF applications.

GrammarKit Assemblies

GrammarKit includes WPF compatible .NET assemblies for .NET Framework and .NET to support most applications' target framework. If you need a specific target framework, then contact Chant Product Support.

To access GrammarKit .NET classes within a WPF application, add project references to the following nuget packages depending on desired speech APIs:

  • Select the application project in the Solution Explorer
  • Right-click the mouse and select the Manage Nuget Packages… menu item.
  • Enter Chant in the search bar.
  • For System.Speech and Microsoft.Speech speech technologies:
    • Select the Chant.GrammarKit package and press the Install button.
  • Chant GrammarKit package
  • For Microsoft SAPI, Microsoft Speech Platform, and Microsoft WindowsMedia (via WinRT C++) speech technologies:
    • Select the Chant.GrammarKit.Windows package and press the Install button.
  • Chant GrammarKit Windows package
  • For Microsoft WindowsMedia (via WinRT .NET) speech technologies:
    • Select the Chant.GrammarKit.WinRT.WindowsMedia package and press the Install button.
  • Chant GrammarKit WinRT WindowsMedia package

To access the GrammarKit .NET classes within applications that target Microsoft SAPI5, Micosoft Speech Platform, and Microsoft WindowsMedia (via WinRT C++) speech technologies, add references to the Chant shared and GrammarKit assemblies in your code.


using System;
...
using Chant.GrammarKit.Windows;
using Chant.Shared.Windows;

Imports Chant.GrammarKit.Windows
Imports Chant.Shared.Windows
Class MainWindow
...

These applications require the applicable GrammarKit Speech API library in the same directory as the application .exe.

Speech API GrammarKit Speech API class GrammarKit Speech API library
Microsoft SAPI 5NSAPI5RecognizerCGrammarKit.SAPI5.dll or CGrammarKitX64.SAPI5.dll
Microsoft Speech PlatformNMSPRecognizerCGrammarKit.MSP.dll or CGrammarKitX64.MSP.dll
Microsoft WindowsMedia (WinRT C++)NWindowsMediaRecognizerCGrammarKit.WinRT.dll or CGrammarKitX64.WinRT.dll

To access the GrammarKit .NET classes within applications that target Microsoft System.Speech and Microsoft.Speech speech technologies, add references to the Chant shared and GrammarKit assemblies in your code.


using System;
...
using Chant.GrammarKit;
using Chant.Shared;

Imports Chant.GrammarKit
Imports Chant.Shared
Class MainWindow
...

These applications do not require a GrammarKit Speech API library.

Speech API GrammarKit Speech API class GrammarKit Speech API library
Microsoft .NET System.SpeechNSAPI5RecognizerNA
Microsoft .NET Microsoft.SpeechNMSPRecognizerNA

To access the GrammarKit .NET classes within applications that target Microsoft WindowsMedia (via WinRT .NET) speech technologies, add references to the Chant shared and GrammarKit assemblies in your code.


using System;
...
using Chant.GrammarKit.WinRT.WindowsMedia;
using Chant.Shared.WinRT.WindowsMedia;

Imports Chant.GrammarKit.WinRT.WindowsMedia
Imports Chant.Shared.WinRT.WindowsMedia
Class MainWindow
...

These applications do not require a GrammarKit Speech API library.

Speech API GrammarKit Speech API class GrammarKit Speech API library
Microsoft WindowsMedia (via WinRT .NET)NWindowsMediaRecognizerNA

Platform Target

For applications that require the applicable GrammarKit Speech API library, set the project Platform target to either x86 for 32-bit application or x64 for 64-bit application as follows:

Within your C# project

  1. Select the Build tab under project properties.
  2. Select x86 or x64 in the Platform target dropdown list.

Within your VB .NET Project

  1. Select the Compile tab under project properties.
  2. Press the Advanced Compile Options... button.
  3. Select x86 or x64 in the Target CPU dropdown list.

Object Instantiation

Declare a global variable for the GrammarKit class, instantiate an instance, add the event handler, and set the credentials.


private NGrammarKit _GrammarKit = null;
private NSAPI5Recognizer _Recognizer = null;
public MainWindow()
{
    InitializeComponent();
    // Instantiate GrammarKit
    _GrammarKit = new NGrammarKit();
    if (_GrammarKit != null)
    {
        // Set credentials
        _GrammarKit.SetCredentials("Credentials");
        _Recognizer = _GrammarKit.CreateSAPI5Recognizer();
        if (_Recognizer != null)
        {
            _Recognizer.CompileDone += this.Compile_CompileDone;
            _Recognizer.CompileError += this.Compile_CompileError;
        }
    }
}

Dim _GrammarKit As NGrammarKit
Dim WithEvents _Recognizer As NSAPI5Recognizer
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    ' Instantiate GrammarKit
    _GrammarKit = New NGrammarKit()
    If (_GrammarKit IsNot Nothing) Then
        ' Set credentials
        _GrammarKit.SetCredentials("Credentials")
        _Recognizer = _GrammarKit.CreateSAPI5Recognizer()
    End If
    ...
End Sub

Event Callbacks

Event callbacks are the mechanism in which the class object sends information back to the application such as compilation is complete or there was an error.


private void Compile_CompileDone(object sender, CompileEventArgs e)
{
    ...
}
private void Compile_CompileError(object sender, CompileEventArgs e)
{
    // Format transcript message
    string errorMsg = "";
    errorMsg = "(" + e.Error.ToString() + ") ";
    errorMsg += "error " + e.LineNumber.ToString() + " ";
    errorMsg += e.Description;
    ...
}

Private Sub Compile_CompileDone(ByVal sender As System.Object, ByVal e As CompileEventArgs) Handles _Recognizer.CompileDone
    ...
End Sub
Private Sub Compile_CompileError(ByVal sender As System.Object, ByVal e As CompileEventArgs) Handles _Recognizer.CompileError
    Dim errorMsg As String
    ' Format transcript message
    errorMsg = "(" + e.Error.ToString() + ") "
    errorMsg += "error " + e.LineNumber.ToString() + " "
    errorMsg += e.Description
    ...
End Sub

Development and Deployment Checklist

When developing and deploying .NET applications, ensure you have a valid license, bundle the correct Chant class libraries, and configure your installation properly on the target system.

Review the following checklist before deploying .NET Applications targeting Microsoft SAPI, Microsoft Speech Platform, or Microsoft WindowsMedia (via WinRT C++) speech technologies:

  • Develop and deploy .NET applications to any system with a valid license from Chant. See the section License for more information about licensing Chant software.
  • Copy Chant.GrammarKit.Windows.dll and Chant.Shared.Windows.dll assemblies to the target system or merge them with your application using an obfuscator like .NET Reactor by Eziriz.
  • Copy applicable 32-bit GrammarKit Speech API DLL(s) to the target system and place in the same directory with your application.
  • Copy NGrammarKit.dll to the target system,
    • register as a COM library on the target system, or
    • place in the same directory with .NET application and include an App.manifest with a dependent assembly declaration:
      
      <dependency>
          <dependentAssembly>
                  <assemblyIdentity type="win32" name="Chant.GrammarKit.Windows" version="10.0.0.0" publicKeyToken="90fe21044a41aa86" />
          </dependentAssembly>
      </dependency>
      
  • Develop and deploy .NET applications to any system with a valid license from Chant. See the section License for more information about licensing Chant software.
  • Copy Chant.GrammarKit.Windows.dll and Chant.Shared.Windows.dll assemblies to the target system or merge them with your application using an obfuscator like .NET Reactor by Eziriz.
  • Copy applicable 64-bit GrammarKit Speech API DLL(s) to the target system and place in the same directory with your application.
  • Copy NGrammarKitX64.dll to the target system,
    • register as a COM library on the target system, or
    • place in the same directory with .NET application and include an App.manifest with a dependent assembly declaration:
      
      <dependency>
          <dependentAssembly>
                  <assemblyIdentity type="win32" name="Chant.GrammarKit.Windows" version="10.0.0.0" publicKeyToken="90fe21044a41aa86" />
          </dependentAssembly>
      </dependency>
      

Review the following checklist before deploying .NET Applications targeting System.Speech or Microsoft.Speech speech technologies:

  • Develop and deploy .NET applications to any system with a valid license from Chant. See the section License for more information about licensing Chant software.
  • Copy Chant.GrammarKit.dll and Chant.Shared.dll assemblies to the target system or merge them with your application using an obfuscator like .NET Reactor by Eziriz.

Review the following checklist before deploying .NET Applications targeting WindowsMedia (via WinRT .NET) speech technologies:

  • Develop and deploy .NET applications to any system with a valid license from Chant. See the section License for more information about licensing Chant software.
  • Copy Chant.GrammarKit.WinRT.WindowsMedia.dll and Chant.Shared.WinRT.WindowsMedia.dll assemblies to the target system or merge them with your application using an obfuscator like .NET Reactor by Eziriz.

Sample Projects

.NET WPF sample projects that use nuget packages may be found in the following directory of the vssamples.zip download:

  • Applications that target Microsoft System.Speech and Microsoft Microsoft.Speech speech technologies:
    • Chant\GrammarKit 10\NET\NET\cs and
    • Chant\GrammarKit 10\NET\NET\vb.
  • Applications that target Microsoft SAPI5, Micosoft Speech Platform, and Microsoft WindowsMedia (via WinRT C++) speech tecnologies:
    • Chant\GrammarKit 10\NET\Windows\cs and
    • Chant\GrammarKit 10\NET\Windows\vb.
  • Applications that target Microsoft WindowsMedia (via WinRT .NET) speech tecnologies:
    • Chant\GrammarKit 10\NET\WinRT\cs and
    • Chant\GrammarKit 10\NET\WinRT\vb.