Persilsoft.Blazor.JSInterop 1.0.2

There is a newer version of this package available.
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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Persilsoft.Blazor.JSInterop" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Persilsoft.Blazor.JSInterop --version 1.0.2                
#r "nuget: Persilsoft.Blazor.JSInterop, 1.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
1.0.7 273 10/16/2024
1.0.6 550 8/18/2024
1.0.5 110 8/18/2024
1.0.4 448 7/19/2024
1.0.3 109 7/9/2024
1.0.2 574 5/21/2024
1.0.1 801 4/24/2024
1.0.0 495 4/18/2024