Important
デバイス メタデータは非推奨となり、Windows の今後のリリースで削除される予定です。 この機能の置き換えについては、「 ドライバー パッケージ コンテナー メタデータ」を参照してください。
Windows 8.1 では、UWP デバイス アプリは、印刷ヘッドの配置やノズルのクリーニングなど、プリンターのメンテナンスを実行できます。 この記事では、C# バージョンの 印刷ジョブ管理とプリンター のメンテナンス サンプルを使用して、双方向通信 (Bidi) を使用してこのようなデバイス メンテナンスを実行する方法を示します。 UWP デバイス アプリ全般の詳細については、「UWP デバイス アプリを使用する」を参照してください。
C# バージョンの印刷ジョブ管理およびプリンター メンテナンス サンプルでは、DeviceAppForPrinters2 プロジェクトのDeviceMaintenance.xaml.cs ファイルを使用したプリンターのメンテナンスを示します。 To work with Bidi, the sample uses the printer extension library in the PrinterExtensionLibrary project. プリンター拡張ライブラリは、v4 印刷ドライバーのプリンター拡張インターフェイスにアクセスするための便利な方法を提供します。 詳細については、 プリンター拡張ライブラリの概要を参照してください。
Note
この記事に示すコード例は、 印刷ジョブ管理とプリンターのメンテナンス サンプルの C# バージョンに基づいています。 このサンプルは、JavaScript と C++ でも使用できます。 C++ は COM に直接アクセスできるため、サンプルの C++ バージョンにはコード ライブラリ プロジェクトは含まれません。 サンプルをダウンロードして、最新バージョンのコードを確認します。
Printer maintenance
Windows 8.1 introduces new printer extension interfaces in the v4 printer driver that you can use for implementing device maintenance: IPrinterBidiSetRequestCallback, IPrinterExtensionAsyncOperation , and IPrinterQueue2. これらのインターフェイスを使用すると、デバイスとプロトコル固有のコマンドに変換してプリンターに送信できるように、Bidi 要求をポート モニターに非同期的に送信できます。 詳細については、「 デバイスのメンテナンス (v4 プリンター ドライバー)」を参照してください。
Tip
C# アプリと JavaScript アプリは、COM API を直接操作できません。 C# または JavaScript UWP デバイス アプリを作成する場合は、プリンター拡張機能ライブラリを使用してこれらのインターフェイスにアクセスします (この記事に示すように)。
Prerequisites
開始する前に、以下の操作を行います。
v4 プリンター ドライバーを使用してプリンターがインストールされていることを確認します。 詳細については、「 v4 印刷ドライバーの開発」を参照してください。
開発用 PC をセットアップします。 See Getting started for info about downloading the tools and creating a developer account.
アプリをストアに関連付けます。 詳細については、「 UWP デバイス アプリの作成 」を参照してください。
プリンターをアプリに関連付けるプリンターのデバイス メタデータを作成します。 詳細については、「 デバイス メタデータの作成」を参照してください。
アプリのメイン ページの UI をビルドします。 すべての UWP デバイス アプリはスタート画面から起動でき、全画面表示で表示されます。 スタート エクスペリエンスを使用して、デバイスの特定のブランドと機能に一致する方法で製品またはサービスを強調表示します。 使用できる UI コントロールの種類に特別な制限はありません。 全画面表示エクスペリエンスの設計を開始するには、 Microsoft Store の設計原則を参照してください。
If you're writing your app with C# or JavaScript, add the PrinterExtensionLibrary project to your UWP device app solution. このプロジェクトは、 印刷ジョブの管理とプリンターのメンテナンス サンプルで確認できます。
Note
C++ は COM に直接アクセスできるため、C++ アプリでは、COM ベースのプリンター デバイス コンテキストを操作するために別のライブラリは必要ありません。
手順 1: Bidi 要求を準備する
デバイス メンテナンス インターフェイスでは、Bidi 要求が文字列形式の XML データである必要があります。 アプリで意味のある場所であればどこでも、Bidi 要求を作成できます。 たとえば、Bidi 要求を文字列定数として保存したり、ユーザー入力に基づいて動的に作成したりできます。
印刷ジョブの管理とプリンターのメンテナンスのサンプルでは、OnNavigatedToメソッドで既定の要求を作成します。 For more info about Bidi, see Bidirectional Communications.
This example is from the OnNavigatedTo method of the DeviceMaintenance.xaml.cs file.
string defaultBidiQuery =
"<bidi:Set xmlns:bidi=\"http://schemas.microsoft.com/windows/2005/03/printing/bidi\">\r\n" +
" <Query schema='\\Printer.Maintenance:CleanHead'>\r\n" +
" <BIDI_BOOL>false</BIDI_BOOL>\r\n" +
" </Query>\r\n" +
"</bidi:Set>";
手順 2: プリンターを検索する
アプリでプリンターにコマンドを送信するには、まずプリンターを見つける必要があります。 これを行うには、印刷ジョブの管理とプリンターのメンテナンスのサンプルには、(PrinterEnumeration ファイル内の) という名前のクラスが含まれています。 このクラスは、デバイス メタデータを使用してアプリに関連付けられているすべてのプリンターを検索し、各プリンターの名前とデバイス ID を含む PrinterInfo オブジェクトの一覧を返します。
This example is from the EnumeratePrinters_Click method of the DeviceMaintenance.xaml.cs file. このサンプルでは、 PrinterEnumeration クラスを使用して、関連付けられているプリンターの一覧を取得する方法を示します。
private async void EnumeratePrinters_Click(object sender, RoutedEventArgs e)
{
try
{
rootPage.NotifyUser("Enumerating printers. Please wait", NotifyType.StatusMessage);
// Retrieve the running app's package family name, and enumerate associated printers.
string currentPackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName;
// Enumerate associated printers.
PrinterEnumeration pe = new PrinterEnumeration(currentPackageFamilyName);
List<PrinterInfo> associatedPrinters = await pe.EnumeratePrintersAsync();
// Update the data binding source on the combo box that displays the list of printers.
PrinterComboBox.ItemsSource = associatedPrinters;
if (associatedPrinters.Count > 0)
{
PrinterComboBox.SelectedIndex = 0;
rootPage.NotifyUser(associatedPrinters.Count + " printers enumerated", NotifyType.StatusMessage);
}
else
{
rootPage.NotifyUser(DisplayStrings.NoPrintersEnumerated, NotifyType.ErrorMessage);
}
}
catch (Exception exception)
{
rootPage.NotifyUser("Caught an exception: " + exception.Message, NotifyType.ErrorMessage);
}
}
Tip
For more info about the PrinterEnumeration and PrinterInfo classes, see the PrinterEnumeration.cs file.
手順 3: Bidi 要求を送信する
Bidi 要求を送信するには、デバイス メンテナンス インターフェイスに Bidi 文字列とコールバックが必要です。
SendBidiRequest_Click メソッドでは、最初にPrinterInfo オブジェクトを使用して、contextという名前のプリンター拡張コンテキスト オブジェクトを作成します。 その後、 PrinterBidiSetRequestCallback オブジェクトが作成され、コールバックの OnBidiResponseReceived イベントを処理するイベント ハンドラーが追加されます。 最後に、プリンター拡張コンテキストの SendBidiSetRequestAsync メソッドを使用して、Bidi 文字列とコールバックを送信します。
This example is from the SendBidiRequest_Click method of the DeviceMaintenance.xaml.cs file.
private void SendBidiRequest_Click(object sender, RoutedEventArgs e)
{
try
{
PrinterInfo queue = (PrinterInfo)PrinterComboBox.SelectedItem;
// Retrieve a COM IPrinterExtensionContext object, using the static WinRT factory.
// Then instantiate one "PrinterExtensionContext" object that allows operations on the COM object.
Object comContext = Windows.Devices.Printers.Extensions.PrintExtensionContext.FromDeviceId(queue.DeviceId);
PrinterExtensionContext context = new PrinterExtensionContext(comContext);
// Create an instance of the callback object, and perform an asynchronous 'bidi set' operation.
PrinterBidiSetRequestCallback callback = new PrinterBidiSetRequestCallback();
// Add an event handler to the callback object's OnBidiResponseReceived event.
// The event handler will be invoked once the Bidi response is received.
callback.OnBidiResponseReceived += OnBidiResponseReceived;
// Send the Bidi "Set" query asynchronously.
IPrinterExtensionAsyncOperation operationContext
= context.Queue.SendBidiSetRequestAsync(BidiQueryInput.Text, callback);
// The 'operationContext' object can be used to cancel the operation if required.
}
catch (Exception exception)
{
rootPage.NotifyUser("Caught an exception: " + exception.Message, NotifyType.ErrorMessage);
}
}
手順 4: Bidi の応答を受け取る
Bidi の "set" 操作が完了すると、 PrinterBidiSetRequestCallback型のコールバック オブジェクトが呼び出されます。 このコールバックは、HRESULT 応答からのエラー処理を処理し、イベント パラメーターを介して Bidi 応答を送信して、 OnBidiResponseReceived イベントをトリガーします。
This example shows the PrinterBidiSetRequestCallback class definition in the DeviceMaintenance.xaml.cs file.
internal class PrinterBidiSetRequestCallback : IPrinterBidiSetRequestCallback
{
/// <summary>
/// This method is invoked when the asynchronous Bidi "Set" operation is completed.
/// </summary>
public void Completed(string response, int statusHResult)
{
string result;
if (statusHResult == (int)HRESULT.S_OK)
{
result = "The response is \r\n" + response;
}
else
{
result = "The HRESULT received is: 0x" + statusHResult.ToString("X") + "\r\n" +
"No Bidi response was received";
}
// Invoke the event handlers when the Bidi response is received.
OnBidiResponseReceived(null, result);
}
/// <summary>
/// This event will be invoked when the Bidi 'set' response is received.
/// </summary>
public event EventHandler<string> OnBidiResponseReceived;
}
その後、Bidi 応答が OnBidiResponseReceived メソッドに送信されます。ここで、 Dispatcher を使用して UI スレッドに結果が表示されます。
This example is from the OnBidiResponseReceived method of the DeviceMaintenance.xaml.cs file.
internal async void OnBidiResponseReceived(object sender, string bidiResponse)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
BidiResponseOutput.Text = bidiResponse;
});
}
Testing
UWP デバイス アプリをテストする前に、デバイス メタデータを使用してプリンターにリンクする必要があります。
デバイス アプリ情報をプリンターに追加するには、プリンターのデバイス メタデータ パッケージのコピーが必要です。 デバイス メタデータがない場合は、「UWP デバイス アプリのデバイス メタデータを作成する」の記事の説明に従って、デバイス メタデータ作成ウィザードを使用して作成できます。
Note
デバイス メタデータ作成ウィザードを使用するには、この記事の手順を完了する前に、Microsoft Visual Studio Professional、Microsoft Visual Studio Ultimate、またはスタンドアロン SDK for Windows 8.1 をインストールする必要があります。 Microsoft Visual Studio Express for Windows をインストールすると、ウィザードを含まないバージョンの SDK がインストールされます。
次の手順では、アプリをビルドし、デバイス メタデータをインストールします。
テスト署名を有効にします。
%ProgramFiles(x86)%\Windows Kits\8.1\bin\x86 から、DeviceMetadataWizard.exe をダブルクリックしてデバイス メタデータ作成ウィザードを起動してください。
From the Tools menu, select Enable Test Signing.
コンピューターを再起動する
ソリューション (.sln) ファイルを開いてソリューションをビルドします。 F7 キーを押すか、サンプルが読み込まれた後、上部のメニューから Build->Build ソリューション に移動します。
プリンターを取り外してアンインストールします。 この手順は、次回デバイスが検出されるときに更新されたデバイス メタデータを Windows が読み取るようにする必要があります。
デバイス メタデータを編集して保存します。 デバイス アプリをデバイスにリンクするには、デバイス アプリをデバイスに関連付ける必要があります。 デバイス メタデータがまだ作成されていない場合は、「 UWP デバイス アプリのデバイス メタデータを作成する」を参照してください。
デバイス メタデータ作成ウィザードがまだ開いていない場合は、DeviceMetadataWizard.exeをダブルクリックして、%ProgramFiles(x86)%\Windows Kits\8.1\bin\x86 から起動します。
[ デバイス メタデータの編集] を選択します。 このオプションを使用すると、既存のデバイス メタデータ パッケージを編集できます。
In the Open dialog box, locate the device metadata package associated with your UWP device app. (It has a devicemetadata-ms file extension.)
[ UWP デバイス アプリ情報の指定 ] ページで、[ UWP デバイス アプリ] ボックスに Microsoft Store アプリ情報を入力します。 [ UWP アプリ マニフェスト ファイルのインポート] を選択すると、 パッケージ名、 発行元名、 UWP アプリ ID が自動的に入力されます。
If your app is registering for printer notifications, fill out the Notification handlers box. In Event ID, enter the name of the print event handler. In Event Asset, enter the name of the file where that code resides.
When you're done, select Next until you get to the Finish page.
[ デバイス メタデータ パッケージの確認 ] ページで、すべての設定が正しいことを確認し、[ ローカル コンピューター上のメタデータ ストアにデバイス メタデータ パッケージをコピー する] チェック ボックスをオンにします。 Then select Save.
デバイスが接続されたときに Windows が更新されたデバイス メタデータを読み取るようにプリンターを再接続します。