Git Product home page Git Product logo

Comments (7)

danroth27 avatar danroth27 commented on July 20, 2024 1

@guardrex I can't think of anything more relaxing than writing Blazor docs! 😄

from blazor.docs.

danroth27 avatar danroth27 commented on July 20, 2024

Improved .NET and JavaScript interoperability.

In some scenarios, when building a Blazor application you'll have to invoke JavaScript functions from .NET and .NET methods from JavaScript code. Blazor includes support for these two scenarios, calling JavaScript functions from C# and calling .NET methods from JavaScript.

Invoking JavaScript functions from .NET

There are times when your Blazor .NET code needs to call some JavaScript code, for example to expose some platform specific capability to the .NET application. For these cases, you can use the T Invoke<T>(functionName, args) and Task<T> InvokeAsync<T>(functionName, args) methods in the RegisteredFunction class to invoke a given set of functions registered with Blazor on the JavaScript side by calling Blazor.registerFunction('functionName',functionImplementation). For example:

Blazor.registerFunction(
    'echo',
    function(message){
        return message;
    });

Blazor.registerFunction(
    'echoAsync',
    function(message){
        return Promise.Resolve(message);
    });
var helloWorld = RegisteredFunction.Invoke<string>("echo","Hello world!");
var helloWorldAsync = await RegisteredFunction.InvokeAsync<string>("echoAsync","Hello world async!");

The JavaScript snippet above, registers the given functions as 'echo' and 'echoAsync' with the Blazor runtime.
Then, on the C# snippet, the functions are invoked by calling Invoke and InvokeAsync respectively passing in the function name (echo and echoAsync) and the desired message to reply back.

Invoking .NET methods from JavaScript

In some scenarios JavaScript code in the browser needs to call .NET methods, for example when a callback in JavaScript is triggered. This can be achieved by using the functions Blazor.invokeDotNetMethod and Blazor.invokeDotNetMethodAsync from JavaScript. These two functions allow JavaScript to call sync and async .NET methods respectively. These methods must be static, non-generic, must not be overloaded and all the method parameters must be concrete types and deserializable using JSON. For example:

namespace Alerts
{
    public class Timeout
    {
        public static void TimeoutCallback()
        {
            Console.WriteLine('Timeout triggered!');
        }
    }
}
Blazor.invokeDotNetMethod({
    type: {
        assembly: 'MyTimeoutAssembly',
        name: 'Alerts.Timeout'
    },
    method: {
        name: 'Timeoutcallback'
    }
})

from blazor.docs.

danroth27 avatar danroth27 commented on July 20, 2024

@rstropek Are you still interested in taking this one on?

If not, @guardrex do you want this one or should I go ahead and put together PR for it?

from blazor.docs.

guardrex avatar guardrex commented on July 20, 2024

If @rstropek is busy, I'll set it up. I'm just rolling around the house bored to death "on vacation" ... whatever that is. 😄

from blazor.docs.

guardrex avatar guardrex commented on July 20, 2024

... and whomever of us does it, we'll cut the content from the Components topic (JS/TS interop section) and link over to this new topic.

from blazor.docs.

danroth27 avatar danroth27 commented on July 20, 2024

This topic should also cover putting the JS interop code in a Blazor class library (dotnet new blazorlib) so you can easily share it as a NuGet package.

from blazor.docs.

guardrex avatar guardrex commented on July 20, 2024

@danroth27 We haven't heard back, so I'm going to work on this one today (Saturday). I should have it submitted either tonight or tomorrow.

from blazor.docs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.