c# - 为什么我不能让这个 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”类型
我不知道为什么。
- 这是一个带有 UWP 包装器的 C# 程序,用于创建 MSIX 包。
- 这似乎是一个非常标准的块,改编自 MS 的各种示例。
- 在 VS 2019 中,我已将该程序与商店应用程序相关联。
- 'trail' 和 'full' 变量似乎正确填充。
- 我已经从不同的位置、构造函数、随机按钮等调用了它。
我的问题...
- 为什么演员会抛出异常?
- 这是一种不再适用的旧方法吗?
- 将 VS 2019 中的包关联到商店应用程序是否会使对 IInitalizeWithWindow 的调用变得多余?
- 如何修复代码以便正确填充“标题”和“价格”?
解决方案
头撞了一堆,我终于让它工作了......
考虑到过去几天没有我没有尝试过的组合/排列,我真的不知道它现在工作的逻辑,但无论如何..
在 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);
其余的现在工作得很好。有趣的是,我已经将应用程序与商店相关联并删除了违规行。这次我一定做了一些不同的事情!
推荐阅读
- mysql - 用于链计数的递归自连接
- python - pandas 为一年中的每个月扩展数据框
- r - 闪亮的 R 通过单击/按钮选择一组项目
- r - df <- mpg, col_name <- hwy, 如何调用 mpg$hwy?df[col_name] 不起作用
- linux - 如何在 curl 命令标题部分中使用变量
- android - Android - 单击 TextInputEditText 时键盘自动消失
- c# - 从非托管 C++ 代码调用 C# 函数时无法处理异常
- swift - AnyPublisher 的 Swift 5.5 异步等待方法
- excel-formula - 根据条件计算唯一值的数量
- python - 如何找到以大写字母开头的任何单词包围的特定预定义单词?