我正在尝试使用Windows运行时组件来提供我的Javascript UWP应用程序和我编写的C#逻辑之间的互操作性.如果我将最低版本设置为Fall Creator的更新(构建16299,需要使用.NET Standard 2.0库),则在尝试调用简单方法时会出现以下错误:
Unhandled exception at line 3, column 1 in ms-appx://ed2ecf36-be42-4c35-af69-93ec1f21c283/js/main.js
0x80131040 - JavaScript runtime error: Unknown runtime error
Run Code Online (Sandbox Code Playgroud)
如果我使用Creator的更新(15063)作为最小值运行此代码,那么代码运行正常.
我创建了一个包含示例解决方案的Github仓库,该解决方案在本地运行时为我生成错误.
这是main.js的样子.尝试运行getExample函数时发生错误:
// Your code here!
var test = new RuntimeComponent1.Class1;
test.getExample().then(result => {
console.log(result);
});
Run Code Online (Sandbox Code Playgroud)
这就是Class1.cs的样子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
namespace RuntimeComponent1
{
public sealed class Class1
{
public IAsyncOperation<string> GetExample()
{
return AsyncInfo.Run(token => Task.Run(getExample));
}
private async Task<string> getExample()
{
return "It's …Run Code Online (Sandbox Code Playgroud) 我有一个包含小数和整数ID的对象列表(使用匿名类型),我正在构建一个函数来获取列表中所有唯一对象中包含的小数位的总和.
这是我写的代码行:
var result = (objectList.Any()) ? objectList.Distinct().Sum(x => x.DecimalValue) : 0;
Run Code Online (Sandbox Code Playgroud)
但是,它始终返回带有值的小数0,即使它应该返回sum - objectList在我的示例中包含五个相同的对象,因此它应该返回DecimalValue.
但是,如果我用这两行代码替换它,它将返回DecimalValue:
decimal result = 0;
if (objectList.Any()) result = objectList.Distinct().Sum(x => x.DecimalValue);
Run Code Online (Sandbox Code Playgroud)
为什么第二组行有效,但第一行返回错误的值?objectList.Any()是的,所以它不应该使用错误的赋值.
更新:我使用以下代码生成了匿名类型列表:
var objectList = _ms.Read(cmd, x => new { DecimalValue = x.Field<decimal>("SomeColumnName"), Id = x.Field<int>("SomeColumnId") }).ToList();
Run Code Online (Sandbox Code Playgroud)
_ms是MS SQL的DAO,它运行lambda函数,将结果的每个数据行转换为匿名类型对象,该对象在列表中返回.我实际上无法弄清楚如何手动构建这些对象的列表.
另一个更新:使用下面的Reed Copsey的回复,我手动创建列表的方式复制了我得到的列表,我可以用手动创建的列表重现错误:
var objectList = Enumerable.Range(0, 5).Select(i => new { DecimalValue = (decimal)31.45, Id = 3 }).ToList();
var result = (objectList.Any()) ? objectList.Distinct().Sum(x => x.DecimalValue) : 0; …Run Code Online (Sandbox Code Playgroud)