0

I am working with universal apps in Visual Studio 2015 since a few days and I have absolutely no clue how to access a c# method in a class since there is no "WebMethod"-Attribute.

    <script type="text/javascript">
    function getWindowsDeviceUUID() {
        //$.ui.popup({
        //    title: "methodcall",
        //    message: "trying to call methode"
        //}
        //    );
        $.ajax({
            type: 'POST',
            url: 'MainPage.xaml.cs/GetDeviceUUID',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (uuid) {
                $.ui.popup({
                    title: "success",
                    message: "called method"
                }
                    );
                return uuid;
            }
        });
    };
</script>

I already tried it with ajax but the "GetDeviceUUID"-method isn't called. However the getWindowsDeviceUUID is.

Some Additional Information: I already have multiple JS & HTML files I have to use in this app. I open the HTML-Sites via a WebView control.

Kind regards

M4s0n
  • 613
  • 1
  • 7
  • 21

2 Answers2

2

Assuming your host app is C# (and your HTML is hosted in a WebView) then you can expose C# methods by calling the AddWebAllowedObject method to inject a C# class that includes the method you want to call. Then from script you can invoke the method via window.whatever-name-you-gave-it.some-method().

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51
  • When I Add an object and I debug the Javascirpt code, the object remains undefined and an exception is thrown because of calling a method of an undefined object.. – M4s0n Aug 17 '15 at 09:41
  • See http://stackoverflow.com/questions/32052364/c-sharp-class-attributes-not-accessible-in-javascript for more information. – M4s0n Aug 18 '15 at 09:44
  • 1
    please update your answer, because the the object is called via `window.whatever-name-you-gave-it.some-method()` – M4s0n Aug 18 '15 at 11:28
  • Thanks -- I removed it! – Peter Torr - MSFT Aug 18 '15 at 14:10
0

I think you just need to change the logic. Rather than calling the class directly, write a function that has access to the class in your code behind then used the WebMethod for your function to use the class. If the class is not accessible even in the code behind, then the class need to be changed.

Erfan Zarger
  • 270
  • 2
  • 7
  • The Problem is there exists no attribute like WebMethod, because the Universal Apps don't use the .Net Framework, but the .Net Core – M4s0n Aug 13 '15 at 14:26