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

2 Answers 2

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().

Sign up to request clarification or add additional context in comments.

4 Comments

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..
please update your answer, because the the object is called via window.whatever-name-you-gave-it.some-method()
Thanks -- I removed it!
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.

1 Comment

The Problem is there exists no attribute like WebMethod, because the Universal Apps don't use the .Net Framework, but the .Net Core

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.