一方、ストアのAPIには、アプリからアプリ自身の更新ができるAPIが用意されています。
このAPIはてっきりUWP用かと思っていたのですが、Desktop Bridgeでも使えるようなので、試してみました。
上のページにあるサンプルを必須部分だけにして、Desktop Bridge用の処理を加えると以下のようになります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using Windows.Services.Store; | |
public async Task DownloadAndInstallAllUpdatesAsync(Window window) | |
{ | |
StoreContext context = StoreContext.GetDefault(); | |
// Get the updates that are available. | |
IReadOnlyList<StorePackageUpdate> updates = await context.GetAppAndOptionalStorePackageUpdatesAsync(); | |
if (updates.Count > 0) | |
{ | |
SetOwnerWindow(context, window); | |
// Download and install the updates. | |
StorePackageUpdateResult result = await context.RequestDownloadAndInstallStorePackageUpdatesAsync(updates); | |
Debug.WriteLine($"Result - {result.OverallState}"); | |
} | |
} | |
[ComImport] | |
[Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")] | |
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] | |
public interface IInitializeWithWindow | |
{ | |
void Initialize(IntPtr hwnd); | |
} | |
public void SetOwnerWindow(StoreContext context, Window window) | |
{ | |
var handle = new WindowInteropHelper(window).Handle; | |
if (handle == IntPtr.Zero) | |
throw new InvalidOperationException(); | |
var initWindow = (IInitializeWithWindow)(object)context; | |
initWindow.Initialize(handle); | |
} |
- StoreContext.GetAppAndOptionalStorePackageUpdatesAsyncメソッドで新しいバージョンのあるパッケージ情報(StorePackageUpdate)を取得して、この有無をチェックする
- この情報を使ってStoreContext.RequestDownloadAndInstallStorePackageUpdatesAsyncメソッドを実行する
1.は、常にパッケージ情報が1つ返ってくるが、このパッケージのバージョン(StorePackageUpdate.Package.Id.Version)は、新しいパッケージのバージョンではなく、現在実行中のパッケージのバージョンを示すので、新しいパッケージがあるかは判別できない。
2.は、このパッケージ情報を使って実行しても現在のパッケージが再インストールされるだけのようで、更新にならない。
これは使い物にならないかなと思いつつ、数少ない先例を見てみると、少し気になることが。
- GetAppAndOptionalStorePackageUpdatesAsync Still Broken
- How Can we Check the New Version of app update is available in store [UWP] programmatically.
その結果、この状態であれば期待どおりに更新に使えることが分かりました。
1.は、新しいパッケージがないときはパッケージ情報が返ってこないので、その有無を捉えればよい。すなわち、以下のように、上のメソッドの前半だけで判別可能。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using Windows.Services.Store; | |
public async Task<bool> CheckAllUpdatesAsync() | |
{ | |
StoreContext context = StoreContext.GetDefault(); | |
IReadOnlyList<StorePackageUpdate> updates = await context.GetAppAndOptionalStorePackageUpdatesAsync(); | |
return (updates.Count > 0); | |
} |
ということで、Desktop Bridgeでも使えることが確認できました。このAPIを利用すればアプリに手動/自動での更新機能を付けられるので便利、なのですが、デバッグを本番環境でやらなければならない、そのために修正の度にパッケージをSubmissonとして公開しなければならないのは、時間的に勘弁してほしいとこです。
0 コメント :
コメントを投稿