AltVBlazor 0.2.0

dotnet add package AltVBlazor --version 0.2.0
NuGet\Install-Package AltVBlazor -Version 0.2.0
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="AltVBlazor" Version="0.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AltVBlazor --version 0.2.0
#r "nuget: AltVBlazor, 0.2.0"
#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 AltVBlazor as a Cake Addin
#addin nuget:?package=AltVBlazor&version=0.2.0

// Install AltVBlazor as a Cake Tool
#tool nuget:?package=AltVBlazor&version=0.2.0

AltV Blazor

A simple Blazor package to help you with event emitting and handling inside of WebViews written using Blazor. The package is meant for Blazor WASM only, however it should also work on Blazor Server, but you might encounter some unexpected behaviour.

Installation

Add nuget package

dotnet add package AltVBlazor --version 0.2.0

Setup

  1. Register AltVBlazor services to your DI container in program.cs
    builder.Services.AddAltVBlazor();
    
  2. Add AltVBlazor server Javascript file to wwwroot/index.html
    <script src="_content/AltVBlazor/AltVBlazorEvents.js"></script>
    

Handling AltV events inside of a Blazor Component

You can either have your component inherit from AltVComponentBase, or inject IAltVEventSubscriber and subscribe to events yourself

Inherit from AltVComponentBase component

To register an event handler, create a method and add JSInvokable and AltVEvent attributes events will be automatically subscribed.

⚠️ IMPORTANT:

if you override OnInitializedAsync make sure to call `base.OnInitializedAsync();' or events will not be subscribed.

@inherits AltVComponentBase

@code {
   private bool chatInputVisible = false;
   private readonly List<ChatMessage> messages = [];
   
   [JSInvokable]
   [AltVEvent("addChatMessage")]
   public void OnChatMessageAdded(string json)
   {
     var chatMessage = ChatMessageExtensions.Deserialize(json);
     
     messages.Add(chatMessage);
     StateHasChanged();
   }
   
   [JSInvokable]
   [AltVEvent("toggleChat")]
   public void OnToggleChat(bool toggle)
   {
     chatInputVisible = toggle;
     StateHasChanged();
   }
}

Inject IAltVEventSubscriber

Inject IAltVEventSubscriber and call IAltVEventSubscriber.SubscribeEventsForComponent when the component is initialized.

@inject IAltVEventSubscriber AltVEventSubscriber

@code {
   private bool chatInputVisible = false;
   private readonly List<ChatMessage> messages = [];
    
   protected override async Task OnInitializedAsync()
   {
     await AltVEventSubscriber.SubscribeEventsForComponent(this);
     await base.OnInitializedAsync();
   }
   
   [JSInvokable]
   [AltVEvent("addChatMessage")]
   public void OnChatMessageAdded(string json)
   {
     var chatMessage = ChatMessageExtensions.Deserialize(json);
     
     messages.Add(chatMessage);
     StateHasChanged();
   }
   
   [JSInvokable]
   [AltVEvent("toggleChat")]
   public void OnToggleChat(bool toggle)
   {
     chatInputVisible = toggle;
     StateHasChanged();
   }
}

Emitting events to client

if your component inherits from AltVComponentBase, use Emit method

private async void OnSubmit(object? sender, string e)
{
    await Emit(ChatWebViewEvents.SubmitChatInput, e);
}

otherwise, inject IAltVEventEmitter

@inject IAltVEventEmitter Emitter

@code {
    private async void OnSubmit(object? sender, string e)
    {
        await Emitter.Emit(ChatWebViewEvents.SubmitChatInput, e);
    }
}
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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.0 167 1/20/2024