Skip to content

Instantly share code, notes, and snippets.

Last active November 21, 2021 06:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mjfusa/6da7e0138d91cd1e9d0ce2f2fb6a7540 to your computer and use it in GitHub Desktop.
Project Reunion 0.5 WinUI Desktop - Store IAP and Window Initialization
using Microsoft.UI.Xaml;
using System;
using System.Runtime.InteropServices;
using Windows.Services.Store;
using WinRT;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see:
namespace App12Reunion
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public interface IInitializeWithWindow
void Initialize(IntPtr hwnd);
public sealed partial class MainWindow : Window
public MainWindow()
public StoreContext m_StoreContext { get; private set; }
private void myButton_Click(object sender, RoutedEventArgs e)
myButton.Content = "Clicked";
public async void DoInAppPurchase()
// initialize m_StoreContext in Window_Activated
var result = await m_StoreContext.RequestPurchaseAsync("9NWMTMH9PNGB");
if (result.Status == StorePurchaseStatus.Succeeded)
textBlock.Text = $"Store purchase succeeded: {result.Status}";
} else
textBlock.Text = $"Store purchase failed: {result.Status}";
bool bActivated = false;
private void Window_Activated(object sender, WindowActivatedEventArgs args)
IntPtr hwnd = PInvoke.User32.GetActiveWindow();
if ( (hwnd == (IntPtr)0) || (bActivated) )
m_StoreContext = StoreContext.GetDefault();
IInitializeWithWindow initializeWithWindowWrapper = ((object)m_StoreContext).As<IInitializeWithWindow>();
bActivated = true;
Copy link

shuice commented Nov 21, 2021

It works, but i do not know why the old style do not works, it saids type cast error.

IInitializeWithWindow initWindow = (IInitializeWithWindow)(object)m_StoreContext;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment