Lionk.Core.Razor 2.0.1

dotnet add package Lionk.Core.Razor --version 2.0.1
                    
NuGet\Install-Package Lionk.Core.Razor -Version 2.0.1
                    
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="Lionk.Core.Razor" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lionk.Core.Razor" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Lionk.Core.Razor" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Lionk.Core.Razor --version 2.0.1
                    
#r "nuget: Lionk.Core.Razor, 2.0.1"
                    
#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.
#:package Lionk.Core.Razor@2.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Lionk.Core.Razor&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Lionk.Core.Razor&version=2.0.1
                    
Install as a Cake Tool

Lionk.Core.Razor

Lionk.Core.Razor is a Blazor-focused extension of the Lionk.Core library, providing essential components and services for dynamically managing and rendering views in a Blazor application. This library integrates MudBlazor components to create interactive, configurable dialogs and dynamic view handling within Blazor.

Overview

The Lionk.Core.Razor library focuses on:

  • Dynamic Component Rendering: Allows rendering of components dynamically based on user interactions and context.
  • View Management: Provides services to locate, register, and manage views of components.
  • Dialog Management: Uses MudBlazor dialogs for displaying views with flexible and reusable UI patterns.

Installation

To use Lionk.Core.Razor in your Blazor project, add the package via NuGet:

dotnet add package Lionk.Core.Razor

Key Components and Services

1. Dynamic Dialogs

The library provides Blazor components for displaying views within MudBlazor dialogs, leveraging MudDialog to present content in a clean and interactive manner.

@using Lionk.Core.Dialog
@using Lionk.Core.View

<MudDialog Style="height:50%">
    <DialogContent>
        <MudCarousel ShowArrows="true"
                     ShowBullets="false"
                     EnableSwipeGesture="true"
                     AutoCycle="false"
                     TData="object"
                     SelectedIndexChanged="ViewChanged"
                     Style="height:100%"
                     SelectedIndex="CurrentIndex">

            @if (ViewDescriptions is not null && ViewDescriptions.Count > 0)
            {
                foreach (ComponentViewDescription viewDescription in ViewDescriptions)
                {
                    <MudCarouselItem Style="padding-left:50px; padding-right:50px; overflow-y: scroll;">
                        <MudGrid Style="width:inherit%">
                            <MudItem>
                                <MudText Typo="Typo.h6">@viewDescription.Name</MudText>
                            </MudItem>
                            <MudItem Style="width:inherit">
                                <DynamicComponentWrapper Type="@viewDescription.ViewType" Parameters="_parameter"/>
                            </MudItem>
                        </MudGrid>
                    </MudCarouselItem>
                }
            }
        </MudCarousel>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Submit">Ok</MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter]
    MudDialogInstance? MudDialog { get; set; }

    [Parameter]
    public object? Component { get; set; }

    [Parameter]
    public List<ComponentViewDescription>? ViewDescriptions { get; set; }

    [Parameter]
    public int CurrentIndex { get; set; }

    private readonly Dictionary<string, object> _parameter = new();

    protected override void OnInitialized()
    {
        if (Component is null) return;
        _parameter.Add("Component", Component);
        if (CurrentIndex >= ViewDescriptions?.Count) CurrentIndex = 0;
    }

    private void Submit()
    {
        MudDialog?.Close(DialogResult.Ok(CurrentIndex));
    }

    private void ViewChanged(int index)
    {
        CurrentIndex = index;
    }
}
Explanation
  • MudDialog: A MudBlazor dialog is used as the main container for the carousel and actions.
  • MudCarousel: Allows navigation between different component views dynamically based on the provided list of ComponentViewDescription.
  • DynamicComponentWrapper: Renders components dynamically based on the ViewType specified in the ComponentViewDescription.

2. Simple Confirmation Dialog

Another example of a dialog is a simpler setup for confirmation actions.

@namespace Lionk.Core.Dialog

<MudDialog>
    <DialogContent>
        <MudText>@ContentText</MudText>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Cancel">Cancel</MudButton>
        <MudButton Color="@Color" Variant="Variant.Filled" OnClick="Submit">@ButtonText</MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter]
    private MudDialogInstance? MudDialog { get; set; }

    [Parameter]
    public string? ContentText { get; set; }

    [Parameter]
    public string? ButtonText { get; set; }

    [Parameter]
    public Color Color { get; set; }

    private void Submit()
    {
        MudDialog?.Close(DialogResult.Ok(true));
    }

    private void Cancel()
    {
        MudDialog?.Cancel();
    }
}

3. View Locator and Registration Services

The library includes services for locating and managing views dynamically within Blazor applications:

IViewLocatorService
  • Provides methods to locate views associated with specific component types and contexts.
  • Supports different view contexts like configuration, detail, page, and widget views.
ViewLocatorService
  • Implements IViewLocatorService to dynamically discover and provide views for components.
  • Uses a types provider (ITypesProvider) to fetch and create views based on defined attributes (ViewOfAttribute).
Example: Locating Views for a Component
using Lionk.Core.View;

public class ExampleComponent { }

public void FindViews()
{
    ITypesProvider typesProvider = new CustomTypesProvider(); // Implement ITypesProvider
    IViewLocatorService viewLocator = new ViewLocatorService(typesProvider);
    
    var views = viewLocator.GetViewOf(typeof(ExampleComponent), ViewContext.Detail);
    foreach (var view in views)
    {
        Console.WriteLine($"View found: {view.Name}, Type: {view.ViewType}");
    }
}

4. Dynamic Component Rendering

The DynamicComponentWrapper component is used to render views dynamically. It integrates with the IViewRegistryService to manage the lifecycle of dynamically rendered views.

Example: Rendering a Dynamic Component
@using Lionk.Core.View

<DynamicComponentWrapper Type="@viewType" Parameters="@viewParameters" />

@code {
    [Parameter]
    public Type? viewType { get; set; }

    [Parameter]
    public IDictionary<string, object>? viewParameters { get; set; }
}

5. View Registry Service

The ViewRegistryService helps in managing active views within the application, allowing checking for active instances and registering or unregistering views dynamically.

Example: Using View Registry Service
using Lionk.Core.View;

var registry = new ViewRegistryService();

// Register a view instance
object viewInstance = new CustomView();
registry.Register(viewInstance);

// Check if a view type has active instances
bool isActive = registry.HasActiveViews(typeof(CustomView));
Console.WriteLine($"Is CustomView active: {isActive}");

// Unregister the view instance
registry.Unregister(viewInstance);

Conclusion

Lionk.Core.Razor provides a robust foundation for dynamic component rendering and view management in Blazor applications. By leveraging MudBlazor for UI components and a flexible service architecture for view management, it allows developers to create interactive and configurable user interfaces easily.

Contribution

Contributions to Lionk.Core are welcome! Please fork the repository, make your changes in a new branch, and submit a pull request.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Lionk.Core.Razor:

Package Downloads
Lionk.Plugin.Blazor

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.1 196 9/5/2024
2.0.0 259 8/28/2024

- Fix concurrency error in cycle graph