Comments (7)
@guardrex I can't think of anything more relaxing than writing Blazor docs! 😄
from blazor.docs.
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.
@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.
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.
... 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.
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.
@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)
- Translate documentation to [pt-br] HOT 2
- Update JS interop to document appropriate use of IJSRuntime with server-side Blazor HOT 1
- Add new sample (Blazor Paint)
- 404 on Get Started :( HOT 1
- Request for new entry in Book section HOT 6
- Add the TabSet example in sample app
- Update Blazor hosting models topic for 0.7.0
- Update Host and deploy for 0.7.0 HOT 1
- Component input validation HOT 5
- Add the doc for bind-property-event syntax HOT 4
- Explain the difference between for and foreach loop in context of lambda expression HOT 4
- Add new sample (Blazor Excel) HOT 11
- Add new sample and library (WebSocket)
- Add section about what is and how to create dynamic components HOT 4
- Broken API link
- Getting started with Blazor - documentation incorrect HOT 3
- Razor Components + EF Core HOT 1
- Redirects
- "What is Blazor" link is broken
- Redirection to newer getting started tutorial HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blazor.docs.