Calling C# component from JavaScript in Windows 10 Universal App
Asked Answered
V

2

6

I'm trying to use Windows Runtime Component (C#) in my Windows 10 Universal App (JavaScript). I found how to do that in Windows 8.x store apps: https://msdn.microsoft.com/en-us/library/hh779077.aspx but this solution is not working with Windows 10 Universal App. It is throwing exception that class is not registered in the JavaScript.

WRC code:

namespace SampleComponent
{
    public sealed class Example
    {
        public static string GetAnswer() 
        { 
            return "The answer is 42."; 
        }

        public int SampleProperty { get; set; }
    }
}

In JS:

 document.getElementById('output').innerHTML =
        SampleComponent.Example.getAnswer();
Veraveracious answered 4/12, 2015 at 10:30 Comment(2)
Can you post some of the code that makes the call from JavaScript to the WRC?Savagery
@Savagery I tried to use code from MSDN example for Widnows 8.x WRC (msdn.microsoft.com/en-us/library/hh779077.aspx#DiveRightIn). Code added above.Veraveracious
S
1

The any CPU mode is not available (by default) if you develop a Windows 10 app written with XAML/C# or an app written with HTML/JS using a WinRT component written with C# because of the ".Net native"

You have to target the good platform :)

Salinas answered 6/12, 2015 at 23:58 Comment(0)
V
1

After some research I found that this problem occurs when building in Any CPU mode. For x86/x64 it is working properly. This solution is enough for me at this moment. I will post more information here if I found how to run it on other platforms.

Veraveracious answered 5/12, 2015 at 17:44 Comment(0)
S
1

The any CPU mode is not available (by default) if you develop a Windows 10 app written with XAML/C# or an app written with HTML/JS using a WinRT component written with C# because of the ".Net native"

You have to target the good platform :)

Salinas answered 6/12, 2015 at 23:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.