locked
Colliding namespaces of WinRT component and WinJS RRS feed

  • Question

  • I am confused about how the winmd wrapper introduces the native code namespace into WinJS.

    I have this namespace definition:

    namespace foo {
      namespace bar {
        public ref class Test sealed {
        };
      }
    }

    and in my JS Project I define methods for the foo namespace also like this:

    WinJS.Namespace.define('foo.FileUtils', {
    });

    Now, it seems the foo.bar.Test coming from the WinRT does not allow any other objects to be defined inside foo. Therefore foo.FileUtils does not get defined and throws an error in base.js initializeProperties because "target" is undefined.

    How are native WinRT component supposed to fit in to the namespace handling of WinJS?

    Friday, March 2, 2012 2:54 PM

Answers

  • Hi Phil,

    So I am able to repro the issue and I do see what the issue is. 

    Technically Javascript does not have a concept of Namespaces.  Really these are simply objects.  You can see this in the debugger actually as you trace through the define function when you declare this.  The objects coming from the WinWT projections (system or your customer winmd) are not javascript objects but more like host objects and have different rules.

    Because of this you cannot have winmd or system host objects with the same name as the WinJS.namespace Objects.  They are not the same thing and this will fail.  For example, try and define something in the Windows namespace (it will also fail).  A host object is added to the global object for each WinMD class reference in your project.  This is not the same as a WinJS objects added when you use the Namespace.define function and the failure is when we attempt to add to one of these pre existing host objects.   I talked to the product group and they confirmed that you need to keep your namespace definitions in your projects separate from any Namespaces imported by WinMD objects.

    Thanks for reporting this!  I am sure this will help others.

    -Jeff


    Jeff Sanders (MSFT)


    Tuesday, March 6, 2012 8:12 PM
    Moderator

All replies

  • Hello,

    I tested this on Windows 8 customer preview, and it works fine. I suggest you to migrate your project to customer preview editor.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, March 5, 2012 10:26 AM
  • Which editor are you talking about? Do you mean VS11 for the CP? I am using VS11 Ultimate, 

    Microsoft Visual Studio 11
    Version 11.0.50214.1 BETAREL

    Could you please provide me with your demo project, so I can test it here?

    Monday, March 5, 2012 5:04 PM
  • Hello,

    I have the same environment with you, Visual Studio 11 and Windows 8 CP.

    But I did not catch the error you mention before. Did you forget to include the C++ reference into your Javascript project.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, March 6, 2012 6:19 AM
  • No, Jesse I did not forget to reference anything. The project runs just fine when the namespace uses and imported from the winrt component is not again defined in js.would you be so kind and show you projects code here, since its to difficult to extract it from my project. I could create a sample project myself but since you already did that we could use ur project as a further base of discussing this problem.

    The problem I describe here is probably also the reason why the MSFT metro app have a Platform winrt component and use a PlatformJS namespace inside their js code.

    Thanks for you help

    Tuesday, March 6, 2012 8:43 AM
  • Hi Phil,

    I understand the issue.  There looks to be some sort of collision with the namespace defined in WinJS and the component.  I am not sure I have ever seen anyone try this before.  I will build a component with the namespace duplication and see what problems I run into this afternoon.  Jesse is in a different TZ so I cannot easily get his sample.  I will post my solution if it works.  If it does not work, I will file an issue for this problem.  It seems you know of a workaround.

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, March 6, 2012 4:18 PM
    Moderator
  • Thank you Jeff! 

    Yes the workaround is as follow: I declare a "foort" namespace instead of "foo" in the WinRT component. Then, in the JS code I define an alias like "bar = foort.bar". 

    Another thing that is annoying for development is that the generated winmd filename has to match the namespace declared in the WinRT component. Because of that limitation one is basically forced to create a new WinRT component for every (nested) namespace you want to expose. You cannot bundle native code in different (top level) namespaces in on WinRT component binary.

    Tuesday, March 6, 2012 4:26 PM
  • Hi Phil,

    So I am able to repro the issue and I do see what the issue is. 

    Technically Javascript does not have a concept of Namespaces.  Really these are simply objects.  You can see this in the debugger actually as you trace through the define function when you declare this.  The objects coming from the WinWT projections (system or your customer winmd) are not javascript objects but more like host objects and have different rules.

    Because of this you cannot have winmd or system host objects with the same name as the WinJS.namespace Objects.  They are not the same thing and this will fail.  For example, try and define something in the Windows namespace (it will also fail).  A host object is added to the global object for each WinMD class reference in your project.  This is not the same as a WinJS objects added when you use the Namespace.define function and the failure is when we attempt to add to one of these pre existing host objects.   I talked to the product group and they confirmed that you need to keep your namespace definitions in your projects separate from any Namespaces imported by WinMD objects.

    Thanks for reporting this!  I am sure this will help others.

    -Jeff


    Jeff Sanders (MSFT)


    Tuesday, March 6, 2012 8:12 PM
    Moderator
  • Thank you for trying and verifying this Jeff!

    I come to think its by design, as all the MS apps currently available in the store that use the nice "Platform" library also have a distinction between "Platform" coming from the WinMD side and "PlatformJS" for definitions done in JS. That aligns perfectly with the "Windows" and "WinJS" we have in the runtime already.

    I think we can live with the workaround.

    Tuesday, March 6, 2012 9:26 PM