locked
Calling a WinMD function from WinJS

    Question

  • I'm having problems calling a function from a managed WinMD library I've created in c#.

    My function definition in the WinMD library is as follows (c#):

    public void DoSomething(IDictionary<String, Object> aDictionary);

    When I try to call this function from my WinJS project, intellisense is showing the following:

    doSomething(Windows.Foundation.CollectionsIMap<String,Object> aDictionary);

    As far as I can tell, there are no classes in the WinJS library that actually implement this interface.

    I have tried JSON

    var myDictionary = { "key":"value" };
    myObject.doSomething(myDictionary);

    and I get this error: 0x800a000d - JavaScript runtime error: Type mismatch

    How can I pass a dictionary into this function?  Thanks!






    • Edited by -steveb Monday, October 15, 2012 9:11 PM missed a piece of my code
    Wednesday, October 10, 2012 11:19 PM

Answers

  • As found by a co-worker who was helping me with this issue, the correct way to pass an IDictionary (IMap) to a managed WinRT library is through a PropertySet.

    var myDictionary = new Windows.Foundation.Collections.PropertySet();
    myDictionary["key"] = "value";
    
    myObject.doSomething(myDictionary);
    Hope this helps someone else.

    • Marked as answer by -steveb Monday, October 15, 2012 9:28 PM
    Monday, October 15, 2012 9:28 PM

All replies

  • Try using an IDictionary or IMap as a parameter to your c# function instead, it might work.

    http://msdn.microsoft.com/en-us/library/windows/apps/br230301.aspx#PassingToManaged

    If that fails, you can pass a JSON string and parse it out once youre in c#



    -Jeff

    Thursday, October 11, 2012 5:36 PM
  • Thanks for the help Jeff.

    Unfortunately I had forgotten that changing the output type of my project from "Class Library" (.dll) to "Windows Runtime Component" (.winmd) had required me to make all of my public methods and class members be IDictionary instead of Dictionary.  So sadly, that isn't the answer.  

    I'm sure that changing it to a string would work, but it seems inconvenient and unnecessary.  I would think there is a way to make a dictionary-ish object that isn't JSON given the WinRT integration into WinJS...

    Anyone?

    Monday, October 15, 2012 9:05 PM
  • As found by a co-worker who was helping me with this issue, the correct way to pass an IDictionary (IMap) to a managed WinRT library is through a PropertySet.

    var myDictionary = new Windows.Foundation.Collections.PropertySet();
    myDictionary["key"] = "value";
    
    myObject.doSomething(myDictionary);
    Hope this helps someone else.

    • Marked as answer by -steveb Monday, October 15, 2012 9:28 PM
    Monday, October 15, 2012 9:28 PM
  • I have tried using a PropertySet but it results in a different error:

    0x80004002 - JavaScript runtime error: No such interface supported

    The WinRT library I'm calling into is a third party. It seems to be managed code. When looking at the interface using ILDASM.exe it looks like Windows.Foundation.Collections.IMap<string,string>. When looking at the interface through Visual Studio, the managed interface is IDictionary<string,string>.

    I'm guessing that PropertySet doesn't work, because it is IMap<string, object> but I can't be sure.

    Any ideas?

    Thanks!
    Guy Arad


    Guyguy

    Monday, May 4, 2015 9:57 AM