首页 > 解决方案 > 为什么我不能让这个 Microsoft Store 代码片段工作?

问题描述

根据 MS 和其他地方的各种示例,我编写了这段测试代码......

    [ComImport]
    [Guid("4AEEEC08-7C92-4456-A0D6-1B675C7AC005")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    private interface IInitializeWithWindow
    {
        void Initialize(IntPtr hwnd);
    }

和..

    private async Task<bool> TestCode()
    {
        StoreContext Store = StoreContext.GetDefault();

        StoreAppLicense licence = await Store.GetAppLicenseAsync();

        bool trial = licence.IsTrial;
        bool full = licence.IsActive;

        IInitializeWithWindow initWindow = (IInitializeWithWindow)(object)Store;
        initWindow.Initialize(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);

        StoreProductResult App = await Store.GetStoreProductForCurrentAppAsync();
        StoreProduct p = App.Product; // Title, price

        string title = p.Title;
        StorePrice price = p.Price;


        return true;
    }

我用 using 来称呼它

bool x = TestCode().Result;

它全部编译并运行,所以我大概添加了所有正确的使用和引用。但是在运行时,该行:

IInitializeWithWindow initWindow = (IInitializeWithWindow)(object)Store;

停止例外..

无法将“Windows.Services.Store.StoreContext”类型的对象转换为“IInitializeWithWindow”类型

我不知道为什么。

我的问题...

  1. 为什么演员会抛出异常?
  2. 这是一种不再适用的旧方法吗?
  3. 将 VS 2019 中的包关联到商店应用程序是否会使对 IInitalizeWithWindow 的调用变得多余?
  4. 如何修复代码以便正确填充“标题”和“价格”?

标签: c#

解决方案


头撞了一堆,我终于让它工作了......

考虑到过去几天没有我没有尝试过的组合/排列,我真的不知道它现在工作的逻辑,但无论如何..

在 UWP 安装程序项目中,我将该项目与 Microsoft Store 中的应用程序相关联,然后删除了以下行:

[ComImport]
[Guid("4AEEEC08-7C92-4456-A0D6-1B675C7AC005")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IInitializeWithWindow
{
    void Initialize(IntPtr hwnd);
}

IInitializeWithWindow initWindow = (IInitializeWithWindow)(object)Store;
initWindow.Initialize(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);

其余的现在工作得很好。有趣的是,我已经将应用程序与商店相关联并删除了违规行。这次我一定做了一些不同的事情!


推荐阅读