0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

App Actions on Windows の Action Provider(URIタイプ)をつくる

Last updated at Posted at 2025-08-23

もくじ
https://tera1707.com/entry/2022/02/06/144447

やりたいこと

先日、COMタイプのAction Providerを作った。

今回は、URIタイプを作りたい。

※また、MSのURIタイプのサンプルは、WinUIで作成されていたが、きっと他の種類のアプリでもできるはずと思うので、コンソールアプリ(をパッケージプロジェクトから動かす)でやってみた。

結論

結論、コンソールアプリでもできた。やった内容を👇にメモする。

やったこと

まずは、COMタイプのAppActionsを作ったときの下準備と同じことをして、準備をする。(wingetでパッケージを入れるのだが、今回はCOMタイプのほうでやったのでskip。)

...下準備というか、「パッケージプロジェクトのPackage.appxManifestの依存ファイルのバージョンを上げる」のところまで、全く同じでよさそうなので、そこまで同じことをやる。

アクション定義のための.jsonファイルを追加する

👇にあるサンプルのjsonの内容を参考に、「RegistActions.json」というファイルをMyActionProviderUriのPJに作成する。

※下記の1行を、「actions」の中に追記した(ForthCoffeというサンプルアプリ、TestingPlaygroundからActionが見えるようにするため)

"allowedAppInvokers": [ "Microsoft.AppActionsTestingPlayground_8wekyb3d8bbwe!App", "FourthCoffee_cw5n1h2txyewy!FourthCoffeeApp" ],

また、input、outputの実験のために、input/outputの名前を少々変えた。

最終的に、下記のようにした。

{
  "version": 2,
  "actions": [
    {
      "id": "ExampleActionProvider.SendMessage",
      "description": "Send a message (URI Launch)",
      "icon": "ms-resource://Files/Assets/LockScreenLogo.png",
      "usesGenerativeAI": false,
      "allowedAppInvokers": [ "Microsoft.AppActionsTestingPlayground_8wekyb3d8bbwe!App", "FourthCoffee_cw5n1h2txyewy!FourthCoffeeApp" ],
      "inputs": [
        {
          "name": "mymessage",
          "kind": "Text"
        }
      ],
      "inputCombinations": [
        {
          "inputs": [
            "mymessage"
          ],
          "description": "Send message '${message.Text}'"
        }
      ],
      "outputs": [
        {
          "name": "myresponse",
          "kind": "Text"
        }
      ],
      "invocation": {
        "type": "Uri",
        "uri": "urilaunchaction-protocol://",
        "inputData": {
          "mymessage": "${message.Text}"
        }
      }
    }
  ]
}

ソリューションエクスプローラーで、「RegistActions.json」ファイルのプロパティで、ビルドアクションを「コンテンツ」にする。また、ビルドアクションを「新しい場合はコピー」に直す。

image.png

パッケージプロジェクトのPackage.appxManifestを編集して、アクションとアプリを紐づける

👇にあるパッケージマニフェストの書き方に沿って、マニフェストファイル(Package.appxmanifest)をいじる。

ファイルはこうなった。

<?xml version="1.0" encoding="utf-8"?>

<Package
  xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
  xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3"
  IgnorableNamespaces="uap rescap">
	<!-- ↑uap3 の部分を追加した -->

	<Identity
	  Name="7c421ba7-34f4-45f1-a2c5-ae16c8bdf60c"
	  Publisher="CN=masa"
	  Version="1.0.0.0" />

	<Properties>
		<DisplayName>MyActionProviderPackageUri</DisplayName>
		<PublisherDisplayName>masa</PublisherDisplayName>
		<Logo>Images\StoreLogo.png</Logo>
	</Properties>

	<!-- ここのバージョンを更新した -->
	<Dependencies>
		<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.22000.0" MaxVersionTested="10.0.22621.0" />
		<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.22000.0" MaxVersionTested="10.0.22621.0" />
	</Dependencies>

	<Resources>
		<Resource Language="x-generate"/>
	</Resources>

	<Applications>
		<Application Id="App"
		  Executable="$targetnametoken$.exe"
		  EntryPoint="$targetentrypoint$">
			<uap:VisualElements
			  DisplayName="MyActionProviderPackageUri"
			  Description="MyActionProviderPackageUri"
			  BackgroundColor="transparent"
			  Square150x150Logo="Images\Square150x150Logo.png"
			  Square44x44Logo="Images\Square44x44Logo.png">
				<uap:DefaultTile Wide310x150Logo="Images\Wide310x150Logo.png" />
				<uap:SplashScreen Image="Images\SplashScreen.png" />
			</uap:VisualElements>

			<!-- ★ここを追加★ -->
			<Extensions>
				<uap:Extension Category="windows.protocol">
					<uap:Protocol Name="urilaunchaction-protocol" ReturnResults="always">
						<!-- app-defined protocol name -->
						<uap:DisplayName>My URI Launch Action Scheme</uap:DisplayName>
					</uap:Protocol>
				</uap:Extension>
				<uap3:Extension Category="windows.appExtension">
					<uap3:AppExtension Name="com.microsoft.windows.ai.actions" DisplayName="MyURILaunchAction" Id="MyUriLaunchActionId" PublicFolder="Assets">
						<uap3:Properties>
							<Registration xmlns="">RegistActions.json</Registration>
						</uap3:Properties>
					</uap3:AppExtension>
				</uap3:Extension>
			</Extensions>

		</Application>
	</Applications>

	<Capabilities>
		<Capability Name="internetClient" />
		<rescap:Capability Name="runFullTrust" />
	</Capabilities>
</Package>

コードを書く準備

Microsoft.AI.Actionsをnugetで入れた。
verは0.1.0。

Microsoft.WindowsAppSDK をnugetで入れた。
verは1.7.2506001。

※WindowsAppSDKは、var eventargs = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();あたりを使うために必要。

image.png

コードを書く

Program.csに、下記のコードを書いた。

using Microsoft.AI.Actions.Helpers;
using Microsoft.Windows.AppLifecycle;
using Windows.AI.Actions;
using Windows.ApplicationModel.Activation;
using Windows.Foundation.Collections;
using Windows.System;

namespace MyActionProviderUri
{
    internal class Program
    {
        internal static ProtocolForResultsOperation? _operation;

        static void Main(string[] args)
        {
            var eventargs = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();

            if ((eventargs != null) && (eventargs.Kind == ExtendedActivationKind.ProtocolForResults))
            {
                ProtocolForResultsActivatedEventArgs? protocolForResultsArgs = eventargs.Data as ProtocolForResultsActivatedEventArgs;

                if (protocolForResultsArgs.CallerPackageFamilyName.EndsWith("_cw5n1h2txyewy"))
                {
                    using (ActionRuntime runtime = ActionRuntimeFactory.CreateActionRuntime())
                    {
                        if (protocolForResultsArgs != null)
                        {
                            ValueSet inputData = protocolForResultsArgs.Data;
                            var message = inputData["mymessage"];

                            Windows.AI.Actions.ActionEntityFactory source = runtime.EntityFactory;
                            Windows.AI.Actions.ActionEntity textEntity = source.CreateTextEntity("レスポンスです。");

                            ValueSet result = new ValueSet();
                            result["myresponse"] = textEntity.Id;

                            _operation = protocolForResultsArgs.ProtocolForResultsOperation;
                            _operation.ReportCompleted(result);
                        }
                    }
                }
            }
        }
    }
}

これで、パッケージPJをスタートアップPJにして、デバッグ実行した。

すると、App Actions Testing Playground の「アクションカタログ」画面に、下記のように今作ったアクションが出てきた。

image.png

アクションを押すと、下のような画面がでて、「アクションの実行」を押すと、

image.png

👇の画面が出てくる。(コード中に書いた「レスポンスです」という応答のもんごんがちゃんと出てる。)

image.png

とりあえず、URIタイプのアクションを、これで動かせた。

感想

COMタイプよりだいぶ簡単な気がする。が、Windows Copilotによると、COMタイプとURIタイプで役割が違うらしいので、どっちがよいというわけでもないかもしれない。

WindowsCopilotによると、下のような違いがあるらしい。

image.png

image.png

ただ、現状、両方を作ってみたが、本当にそのような違いがあるかの実感はない。
(例えば、パラメータ制限がある、と書かれてるが、どう制限がかかるか?はよくわからない。あと、私の書いたzennの記事をCopilotが参考サイトとして挙げているのが、なんか信頼度が怪しく感じてしまう。まだ参考にできそうなページが少ないのか?)

※第一、今回試しに作ったアクションで、inputにTextタイプのエンティティを使って、それはうまくいっているのだが、copilotは「Textのエンティティは使えない」と言ってるっぽい。

やはりちょっと言っていることが怪しめなので、自分で調べてみる必要ありそう。

今回の実験コード

参考

  • Get started with App Actions on Windows
    App Actionsを自作するときの入り口ドキュメント

  • App Actions on Windows のサンプルアプリ
    この中に、ActionProvider、Consumer両方のサンプルがあった。

  • App Actions Testing Playground アプリ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?