'Unable to implement in-app purchase in the Microsoft store for Wpf Core Desktop Bridge
My application is using .NET 5 and Desktop Bridge. Because I'm using Desktop Bridge, I implemented the IInitializeWithWindow interface as many guides online have done.
[ComImport]
[Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInitializeWithWindow
{
void Initialize(IntPtr hwnd);
}
I have a method here that executes an in-app purchase in the Microsoft store:
private StoreContext storeContext = StoreContext.GetDefault();
IInitializeWithWindow initWindow = (IInitializeWithWindow)(object)storeContext;
var ptr = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
//Call the IInitializeWithWindow.Initialize method, and pass the handle of the window
//to be the owner for any modal dialogs that are shown by StoreContext methods.
initWindow.Initialize(ptr);
result = await storeContext.RequestPurchaseAsync(storeID);
The problem is that when I run the application I get the error "Unable to cast object of type 'Windows.Services.Store.StoreContext' to type 'IInitializeWithWindow'".
It's may be important to say that I am using the net5.0-windows10.0.18362.0 TFM (Target Framework). I think that the problem is that I'm using .NET 5 and that it may not be supported.
Solution 1:[1]
Try this solution for .NET 5 Projects.
using Windows.Services.Store;
using WinRT;
StoreContext storeContext = StoreContext.GetDefault();
private async Task Buy()
{
var initWindow = storeContext.As<IInitializeWithWindow>();
initWindow.Initialize(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
var productResult = await storeContext.RequestPurchaseAsync(storeID);
}
Solution 2:[2]
Following piece of code works for .NET 6 project
WinRT.Interop.InitializeWithWindow.Initialize(storeContext, new WindowInteropHelper(window).Handle);
Solution 3:[3]
I just quote the Microsoft documentation
For .NET 5 or later
If your application is written in C# with .NET 5 or later, follow these steps.
- Make sure that the TargetFramework property in the project file is set to a specific Windows SDK version to access the Windows Runtime APIs, which provides access to the WinRT.Interop namespace. For example:
<PropertyGroup>
<!-- You can also target other versions of the Windows SDK and .NET, e.g. "net5.0-windows10.0.19041.0" -->
<TargetFramework>net6.0-windows10.0.22000.0</TargetFramework>
</PropertyGroup>
- Get a StoreContext object by using the GetDefault method (or GetForUser if your app is a multi-user app) as described earlier in this article). To initialize the dialog with the specified window handle, use the WinRT.Interop.WindowNative.GetWindowHandle and WinRT.Interop.InitializeWithWindow.Initialize methods (see Retrieve a window handle (HWND) and Display WinRT UI objects that depend on CoreWindow).
StoreContext context = StoreContext.GetDefault();
// Obtain window handle by passing in pointer to the window object
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(windowObject);
// Initialize the dialog using wrapper funcion for IInitializeWithWindow
WinRT.Interop.InitializeWithWindow.Initialize(context, hwnd);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Engin Kirmaci |
| Solution 3 |
