简体   繁体   中英

Com Type Marshaling - C# Windows Runtime Component - Cordova Project Javascript Call - Cordova Windows Plugin

I'm working on a plugin for a Cordova project. I have a project to build my windows run-time component. I'm trying to pass a dictionary to my javascript code. This works fine if I use bool, or int, but if I use a more complex object type, I get an error. Here is the code from my plugin in c#.

    public static IAsyncOperation<IDictionary<string, int>> doTestFunction()
    {
        return TestFunction().AsAsyncOperation();
    }

    private static async Task<IDictionary<string, int>> TestFunction()
    {
        IDictionary<string, int> testDictionary = new Dictionary<string, int>();
        testDictionary.Add("value1", 1);
        testDictionary.Add("value2", 1);
        return testDictionary;
    }

Here is my javascript call.

BLEComm.doTestFunction(
        function (res) {
            if (res) {
                console.log("it didn't completely fail");
            } else {
                alertOpen("it kind of failed");
            }
        },
        function (err) {
            console.log("failure" + err);
        }
    );

My the error logged to my console:

failureWinRTError: The text associated with this error code could not be found.

ComTypeMarshalling_MissingInteropData index.js (225,17)

Again, if I just return an int or a bool, I don't have any issues. I have searched and searched and can't figure out what's wrong.

So it helps to start with MS examples instead of outsourced examples. Below works.

using System.Runtime.InteropServices.WindowsRuntime;

Here is the code.

    public static IAsyncOperation<IDictionary<string, int>> TestFunction2()
    {
        return AsyncInfo.Run<IDictionary<string, int>>((token) =>
            Task.Run<IDictionary<string, int>>(() =>
            {
                return AddDictionary();
            }, token)
        );
    }

    //private static async Task<IDictionary<string, int>> AddDictionary()
    private static IDictionary<string, int> AddDictionary()
    {
        IDictionary<string, int> testDictionary = new Dictionary<string, int>();
        testDictionary.Add("value1", 23);
        testDictionary.Add("value2", 1986);
        return testDictionary;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM