4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

.NET 5 で Microsoft Store のアプリ内課金の API の呼び方

Last updated at Posted at 2020-10-29

試してみたら動きそうレベルの内容です。

アプリ内課金の API を MSIX 化した WPF や WinForms などのアプリから呼ぶには IInitializeWithWindow というインターフェースを自分で作ってキャストして Initialize メソッドを読んでやる必要があると以下のドキュメントに書いてあります。

アプリ内課金 API を実際に試すのはちょっと荷が重いので、.NET 5 で WPF アプリを作って Target Framework Monikers に net5.0-windows10.0.19041.0 を指定して呼び出すのに同じ手順が必要な FileOpenPicker クラスで試してみたところ IInitializeWithWindow インターフェースへのキャストに失敗してしまいました。

image.png

ではどうするのか?という感じなのですが using WinRT; を追加して As<T> メソッドで IInitializeWithWindow にキャストしてやればいいです。

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using Windows.Storage.Pickers;
using WinRT;

namespace WpfApp2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            var picker = new FileOpenPicker();
            // It doesn't work on .NET 5
            // ((IInitializeWithWindow)(object)picker).Initialize(...);
            var w = picker.As<IInitializeWithWindow>();
            w.Initialize(new WindowInteropHelper(this).Handle);

            picker.FileTypeFilter.Add(".png");
            var file = await picker.PickSingleFileAsync();
            MessageBox.Show(file?.Name ?? "not selected");
        }
    }

    [ComImport]
    [Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IInitializeWithWindow
    {
        void Initialize(IntPtr hwnd);
    }
}

同じ要領でアプリ内課金の API も叩けそうだという雰囲気。

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?