locked
Encapsulated a Ported View Model in C#, Now I want to use it in JavaScript?

    Question

  • I ported a portion of our existing ASP MVC application into a Metro Styled Class Library Project. The library returns a dynamic json view model. I am stuck no understanding the next steps to access this library from my JavaScript code in my main Metro UX project. Adding a project reference, warns that this is the right way top do it. So I am trying to understand how I bubble up the interfaces into a JavaScript include for use in my project. Any pointers are appreciated.

    Larouex

    Saturday, September 17, 2011 3:24 AM

Answers

  • Hi Larouex,

    The step I was missing was to make my Output type: of the C# class to be WinMD file.  You also want to make sure the class is sealed:

    public sealed class Class1

    Finally you can add the solution to your jscript based WWA app and then include a reference to that solution to get the class to appear and work in you jscript.

    Easy peasey!  Let me know if you have any problems.


    Jeff Sanders (MSFT)
    Monday, September 19, 2011 6:32 PM
    Moderator

All replies

  • If I understand your question, you want to expose your C# library as an extension SDK.  This tutorial walks you through this:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh441589(v=VS.85).aspx

    The difference is that you would reference your extension in a js project instead of a C# project as they illustrate in the Tutorial.

    Let me know if this helps!

    I take that back.  I created a simple C# lib and included it and cannot reference it in the JS project.  It appears it is not a valid combination or there is a problem with the preview project.

    I get an error:

    ---------------------------
    Confirm unsupported reference(s)
    ---------------------------
    One or more of the selected items is not a valid reference for this type of project.  Do you want to continue and add all the references anyway?
    ---------------------------
    OK   Cancel  
    ---------------------------

    I can add a C++ WinRt component with no component and C# to C# or VB.  I will see if I can find out why C# to JS is a problem for you!

     

     

     

     


    Jeff Sanders (MSFT)
    Monday, September 19, 2011 4:41 PM
    Moderator
  • Hi Larouex,

    The step I was missing was to make my Output type: of the C# class to be WinMD file.  You also want to make sure the class is sealed:

    public sealed class Class1

    Finally you can add the solution to your jscript based WWA app and then include a reference to that solution to get the class to appear and work in you jscript.

    Easy peasey!  Let me know if you have any problems.


    Jeff Sanders (MSFT)
    Monday, September 19, 2011 6:32 PM
    Moderator
  • Hi Larouex,

    The step I was missing was to make my Output type: of the C# class to be WinMD file.  You also want to make sure the class is sealed:

    public sealed class Class1

    Finally you can add the solution to your jscript based WWA app and then include a reference to that solution to get the class to appear and work in you jscript.

    Easy peasey!  Let me know if you have any problems.


    Jeff Sanders (MSFT)


    Hi Jeff,

    I've tried to do this - including adding the sealed keyword - and it appears in Intellisense, but at runtime I get a JavaScript runtime error that the class is not registered. I've added it as a reference, but didn't modify the HTML to include a reference to the WinMD file. What am I missing?

    Wednesday, September 28, 2011 3:37 PM
  • Well, how about we take this step by step to narrow down the issue?  Try and create a Basic Jscript app and create a simple C# class (ensure the output type is WinMD) and see if you can get that to work.  Then work up to your ported code.  You must be doing something different or there is something in your C# library that is causing the issue.
    Jeff Sanders (MSFT)
    Wednesday, September 28, 2011 3:41 PM
    Moderator
  • I just got the class not registered error and solved it by removing and re-adding the reference to the c# classlibrary. I'm guessing that it wasn't a WinMD file when it was first added and it isn't auto-refreshing the reference.

    Tim Greenfield

    Saturday, October 01, 2011 10:12 PM