Persilsoft.Blazor.JSInterop
1.0.2
See the version list below for details.
dotnet add package Persilsoft.Blazor.JSInterop --version 1.0.2
NuGet\Install-Package Persilsoft.Blazor.JSInterop -Version 1.0.2
<PackageReference Include="Persilsoft.Blazor.JSInterop" Version="1.0.2" />
paket add Persilsoft.Blazor.JSInterop --version 1.0.2
#r "nuget: Persilsoft.Blazor.JSInterop, 1.0.2"
// Install Persilsoft.Blazor.JSInterop as a Cake Addin #addin nuget:?package=Persilsoft.Blazor.JSInterop&version=1.0.2 // Install Persilsoft.Blazor.JSInterop as a Cake Tool #tool nuget:?package=Persilsoft.Blazor.JSInterop&version=1.0.2
Persilsoft.Blazor.JSInterop
A library containing abstract classes for loading JavaScript and WebAssembly modules
Example for JavaScript module
We have the following JavaScript module named geolocationModule.js with a function that gets the user's coordinates
const getPosition = () => {
return new Promise((resolve, reject) => {
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(returnPosition, returnError);
}
function returnPosition(position) {
resolve({
latitude: position.coords.latitude,
longitude: position.coords.longitude
});
}
function returnError(error) {
let errorMessage;
switch (error.code) {
case error.PERMISSION_DENIED:
errorMessage = "User denied the request for Geolocation.";
break;
case error.POSITION_UNAVAILABLE:
errorMessage = "Location information is unavailable.";
break;
case error.TIMEOUT:
errorMessage = "The request to get user location time out.";
break;
case error.UNKNOWN_ERROR:
errorMessage = "An unknown error ocurred.";
break;
default:
errorMessage = "An error has ocurred.";
}
reject(errorMessage);
}
});
}
export { getPosition };
Now, you need to create a service to interoperate with the JavaScript code. Here the Blazor.JSInterop library makes things easier for us
using Persilsoft.Microsoft.JSInterop;
public record struct GeolocationLatLong(double Latitude, double Longitude);
public class GeolocationService(IJSRuntime jsRuntime)
: JSLoaderServiceBase(jsRuntime, $"./js/geolocationModule.js")
{
public async ValueTask<GeolocationLatLong> GetPosition() =>
await InvokeAsync<GeolocationLatLong>("getPosition");
}
Register the service.
builder.Services.AddScoped<GeolocationService>();
Finally you can use it.
@page "/get-position"
@inject GeolocationService Geolocation
<button class="btn btn-primary mb-2" @onclick=ShowPosition>Show my position</button>
<textarea class="form-control" rows="3" disabled @bind=Result>
</textarea>
@code {
private string Result;
private async Task ShowPosition()
{
var Coords = await Geolocation.GetPosition();
Result = $"Latitude: {Coords.Latitude}, Longitude: {Coords.Longitude}";
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.5)
NuGet packages (7)
Showing the top 5 NuGet packages that depend on Persilsoft.Blazor.JSInterop:
Package | Downloads |
---|---|
Persilsoft.Recaptcha.Blazor
Exposes a service that allows you to obtain a reCAPTCHA token and validate that token through a verification endpoint that you need to add to your backend |
|
Persilsoft.Leaflet.Blazor
A Leaflet service for Blazor and Razor Components applications. |
|
Persilsoft.Nominatim.Geolocation.Blazor
A Geolocation library for Blazor and Razor Components applications. |
|
Persilsoft.SweetAlert.Blazor
Blazor implementation of SweetAlert (a popular Javascript modal popup library) |
|
Persilsoft.Culqi.Blazor
Proporciona un botón como componente para abrir la interfaz de Culqi Checkout. |
GitHub repositories
This package is not used by any popular GitHub repositories.