简体   繁体   English

将Windows运行时组件与Javascript UWP App一起使用时,出现“未知的运行时错误”

[英]“Unknown Runtime Error” when using Windows Runtime Component with Javascript UWP App

I'm trying to use a Windows Runtime Component to provide interoperability between my Javascript UWP app and C# logic that I've written. 我正在尝试使用Windows运行时组件在Javascript UWP应用程序和我编写的C#逻辑之间提供互操作性。 If I set the minimum version to Fall Creator's Update (build 16299, needed to use .NET Standard 2.0 libraries), I get the following error when trying to call a simple method: 如果我将最低版本设置为Fall Creator's Update(内部版本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

If I run this code using Creator's Update (15063) as the minimum, then the code runs fine. 如果我至少使用创建者更新(15063)运行此代码,则该代码可以正常运行。

I've created a Github repo containing a sample solution that generates the error for me when running locally. 我创建了一个包含示例解决方案的Github存储库 ,该示例解决方案在本地运行时会为我生成错误。

Here's what main.js looks like. 这是main.js的样子。 The error occurs when trying to run the getExample function: 尝试运行getExample函数时发生错误:

// Your code here!

var test = new RuntimeComponent1.Class1;

test.getExample().then(result => {
    console.log(result);
});

This is what Class1.cs looks like: 这是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 working";
        }
    }
}

I can't think of a much simpler test case than that - I have no NuGet packages installed or anything like that. 我想不到一个比这简单得多的测试用例-我没有安装NuGet软件包或类似的东西。 I have no idea what might be causing this. 我不知道是什么原因造成的。 Anyone else have ideas? 其他人有想法吗?

There is nothing actually async about this function, even as a simplified example 实际上,此功能没有任何异步,即使是一个简化的示例

private async Task<string> getExample()
{
    return "It's working";
}

Also if the said function is already return a Task then there is no need to wrap it in Task.Run here 另外,如果所说的函数已经返回一个Task则无需将其包装在Task.Run

return AsyncInfo.Run(token => Task.Run(getExample));

Refactor the code to follow advised syntax 重构代码以遵循建议的语法

public sealed class Class1 {
    public IAsyncOperation<string> GetExampleAsync() {
        return AsyncInfo.Run(token => getExampleCore());
    }

    private Task<string> getExampleCore() {
        return Task.FromResult("It's working");
    }
}

Since there is nothing to be awaited, use Task.FromResult to return the Task<string> from the private getExampleCore() function. 由于没有什么可等待的,所以使用Task.FromResult从私有getExampleCore()函数返回Task<string>

Do note also that because the original functions were returning unstarted tasks, that this causes an InvalidOperationException to be thrown by AsyncInfo.Run<TResult>(Func<CancellationToken, Task<TResult>>) Method 还要注意,由于原始函数正在返回未启动的任务,因此这将导致AsyncInfo.Run<TResult>(Func<CancellationToken, Task<TResult>>)方法引发InvalidOperationException

You can also consider taking advantage of the AsAsyncOperation<TResult> extension method, given the simple definition of the called function. 给定调用函数的简单定义,您还可以考虑利用AsAsyncOperation<TResult>扩展方法。

public IAsyncOperation<string> GetExampleAsync() {
    return getExampleCore().AsAsyncOperation();
}

And invoked in JavaScript 并在JavaScript中调用

var test = new RuntimeComponent1.Class1;

var result = test.getExampleAsync().then(
    function(stringResult) {
        console.log(stringResult);
    });

This is not proper async method: 这是不正确的异步方法:

private async Task<string> getExample()
{
    return "It's working";
}

The reason for this is that it was supposed to return Task<string> , not just string . 这样做的原因是应该返回Task<string> ,而不仅仅是返回string

So, you should change it to: 因此,您应该将其更改为:

private async Task<string> getExample()
{
    return Task.FromResult("It's working");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在运行时组件UWP Windows 10 App中从BackgroundTask调用WebApi - WebApi Call from BackgroundTask in RunTime Component UWP Windows 10 App UWP运行时组件-使用图像 - UWP Runtime Component - Using Images UWP C# 和 Windows 运行时组件 C++ 打开文件时出现权限被拒绝错误 - UWP C# and Windows Runtime Component C++ Permission Denied error when opening file UWP,加载 Windows 运行时组件 == 请求的 Windows 运行时类型“...”未注册 - UWP, loading windows runtime component == Requested Windows Runtime type "..." is not registreated 如何在Windows运行时组件(UWP)中调用RequestAccessAsync() - How to call RequestAccessAsync() in a Windows Runtime Component (UWP) 从 UWP 运行时组件项目更新 UWP 应用 UI 的方法 - Way to update UWP App UI from UWP Runtime Component Project 我可以从Windows 10(UWP)应用程序中的Web Worker调用自定义运行时组件吗 - Can I call a custom runtime component from a web worker in a Windows 10 (UWP) app 如何在用 c# 编写的 UWP 应用程序中从 Windows 运行时组件 (c#) 调用函数? - How do you call functions from a Windows Runtime Component (c#) in a UWP app written in c#? 使用JavaScript从Windows运行时组件运行IAsyncOperation - Running IAsyncOperation from a Windows Runtime Component using JavaScript 当UWP解决方案包括Windows运行时组件(C#)时,WACK失败 - WACK fails when UWP solution includes Windows Runtime Component (C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM