c# - InvalidCastException 将 PlayFab.Json.JsonObject 转换为 PlayFab.PlayFabResult C# PlayFab
问题描述
我正在 WPF 下开发一个 C# .Net Core 项目,并与 PlayFab 链接,更具体地说,与 CloudScript 修订版链接。以下代码是 WPF 中 C# 类中的一个函数,该函数在调用 CloudScript 函数后运行。CloudScript 函数购买物品并将其提供给玩家。当函数没有返回错误时,我的问题适用。
这是代码:
/// <summary>
/// PlayFab check query
/// </summary>
/// <param name="result"></param>
private void GetCloudScriptPurchase(ExecuteCloudScriptResult result)
{
JsonObject jsonResult = (JsonObject)result.FunctionResult;
try
{
// This is where I get responseContent
jsonResult.TryGetValue("responseContent", out object responseContent);
shopDataContext.PlFabInfo = (string)responseContent;
// Here, this is what would suit me better, I avoid an API call just by modifying
// the variable with the return of the CloudScript function
jsonResult.TryGetValue("messageValue", out object messageValue);
this.getUserInvent = (PlayFabResult<GetUserInventoryResult>)messageValue;
this.ClientVirtualCurrency(
getUserInvent.Result.VirtualCurrency["BC"]
);
// Here I update the local variable by making another API call,
// which I want to avoid
this.getUserInvent = PlayFabClientAPI.GetUserInventoryAsync(new GetUserInventoryRequest()).Result;
this.ClientVirtualCurrency(
PlayFabClientAPI.GetUserInventoryAsync(
new GetUserInventoryRequest()).Result.Result.VirtualCurrency["BC"]
);
}
catch (Exception Exception)
{
throw Exception;
}
}
这个shopDataContext是一个特定于实际页面的 Binding,这里它只是显示 CloudScript 函数的返回。此ClientVirtualCurrency函数计算在客户端上无错误显示的玩家金额。它需要一个 int 参数。
CloudScript 的返回和代码的返回:
return { responseContent: messageReturn, messageValue: server.GetUserInventory({PlayFabId: currentPlayerId}) }
"FunctionResult": {
"responseContent": "The skin deuxiopack is now yours!",
"messageValue": {
"PlayFabId": "862C4ED0EE57D60F",
"Inventory": [
{
"ItemId": "deuxiopack",
"ItemInstanceId": "63A13435128DA937",
"ItemClass": "Pack",
"PurchaseDate": "2021-06-15T15:10:54.87Z",
"CatalogVersion": "Skin",
"DisplayName": "DeuxioPack",
"UnitPrice": 0
},
{
"ItemId": "trexiopack",
"ItemInstanceId": "504C450155074940",
"ItemClass": "Pack",
"PurchaseDate": "2021-06-11T14:37:18.772Z",
"CatalogVersion": "Skin",
"DisplayName": "TrexioPack",
"UnitPrice": 0
},
{
"ItemId": "starterpack",
"ItemInstanceId": "C191AB8C4A6717C6",
"ItemClass": "Pack",
"PurchaseDate": "2021-06-11T14:29:31.955Z",
"Annotation": "Granted via Game Manager",
"CatalogVersion": "Skin",
"DisplayName": "StarterPack",
"UnitPrice": 0
}
],
"VirtualCurrency": {
"BC": 200
},
"VirtualCurrencyRechargeTimes": {}
}
}
CloudScript 函数返回一个responseContent对象,该对象是要在客户端上显示的成功或错误消息(在我们的例子中是成功消息)的字符串。此函数还返回一个messageValue对象,它是一个PlayFab.Json.JsonObject。
当我尝试时,此错误显示在捕获中:System.InvalidCastException: Unable to cast object of type 'PlayFab.Json.JsonObject' to type 'PlayFab.PlayFabResult`。因此,我无法将PlayFab.Json.JsonObject类型更改为PlayFab.PlayFabResult,以便能够在没有新 API 调用的情况下更新我的本地变量getUserInvent 。
如果有人可以帮助我或给我提示,请提前致谢。
解决方案
推荐阅读
- excel - 图像下载失败时挂起的 Excel VBA 代码的超时或错误处理
- mediawiki - 是否可以在 page1 上创建变量并在 page2 上使用该变量?(不使用扩展)
- java - 如何在 if-else 语句中使用用户输入,如果用户输入不是所要求的,我如何循环代码?
- javascript - 无法将页脚 css 应用于 Angular 应用程序中的页脚对话框
- error-handling - 向最终用户显示或不显示与系统相关的错误消息,这是最佳实践吗?
- python - Python - 如何使用请求和线程循环直到站点关闭
- sorting - 在SAS中合并而不进行proc排序
- python - 如何在 Python 中读取带有标签的 txt 文件?
- asp.net - ASP.NET Webforms OnClientClick 不输入提交
- git - 合并后继续git分支是否常见?