AutoPageStateContainerGenerator 0.0.8

dotnet add package AutoPageStateContainerGenerator --version 0.0.8
                    
NuGet\Install-Package AutoPageStateContainerGenerator -Version 0.0.8
                    
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="AutoPageStateContainerGenerator" Version="0.0.8" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AutoPageStateContainerGenerator" Version="0.0.8" />
                    
Directory.Packages.props
<PackageReference Include="AutoPageStateContainerGenerator" />
                    
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 AutoPageStateContainerGenerator --version 0.0.8
                    
#r "nuget: AutoPageStateContainerGenerator, 0.0.8"
                    
#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 AutoPageStateContainerGenerator@0.0.8
                    
#: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=AutoPageStateContainerGenerator&version=0.0.8
                    
Install as a Cake Addin
#tool nuget:?package=AutoPageStateContainerGenerator&version=0.0.8
                    
Install as a Cake Tool

AutoPageStateContainerGenerator

https://www.cnblogs.com/marvelTitile/p/18902808

介绍

Blazor的组件数据(或者说组件的属性),如果没有做特殊处理的话,在路由切换的时候就会丢失当前页面上的数据,不管是Server还是WebAssembly,官方文档的推荐做法都是将数据抽出来当作一个服务,在页面中注入使用(内存中状态容器服务),而使用生成器,就可以很方便的完成这些功能,自动为Razor组件生成相应的数据容器。

使用

StateContainerAttribute

在需要生成数据容器的组件上标注。

属性
名称 类型 说明
Lifetime Microsoft.Extensions.DependencyInjection.ServiceLifetime 指定容器服务注入的生命周期
Name string? 设置容器名称(用法)
Implements Type? 设置容器自定义实现类型(用法)

SaveStateAttribute

标注需要保存的字段。

AddStateContainers

注入生成的数据容器

父类中使用

必须使用virtual属性

public partial class CounterParent : ComponentBase
{
    [SaveState]
    public virtual string? Type { get; set; } = "CounterParent";

    public void CheckType()
    {
        Console.WriteLine(Type);
    }
}

在其他地方使用内存数据容器

1. 直接从Ioc容器中获取,生成的类型为类型的全名StateContainer

2. 使用IStateContainerManager

设计目的:如果使用第一种方式,主要问题是类型名称太长,并且产生了依赖,所以就出现了IStateContainerManager

Counter为例

public interface ICounter
{
    // 假如我想在其他地方通过ICounter使用数据容器中的Name1
    string? Name1 { get; set; }
}
[StateContainer(Name = "Counter", Implements = typeof(ICounter))]
public partial class Counter
{
    [SaveState]
    public partial string? Name1 { get; set; }

    [SaveState]
    public partial int Index { get; set; }

    [SaveState(Init = "[]")]
    public partial List<string> Values { get; set; }
}

Home组件中使用Counter组件的数据容器

// 注入IStateContainerManager
[Inject, NotNull] IStateContainerManager? ContainerManager { get; set; }
ICounter? counter;
protected override void OnInitialized()
{
    base.OnInitialized();
    // 从IStateContainerManager中获取ICounter
    counter = ContainerManager.GetStateContainer<ICounter>("Counter");
}

示例

Counter组件,不能直接在razor文件中添加该Attribute。

使用字段

[StateContainer(Name = "Counter", Implements = typeof(ICounter))]
public partial class Counter
{
    [SaveState]
    private string? name1;

    [SaveState]
    private int index;

    [SaveState]
    private List<string> values = [];

    public string? Name2 { get; set; }
}

使用分部属性

[StateContainer(Name = "Counter", Implements = typeof(ICounter))]
public partial class Counter
{
    [SaveState]
    public partial string? Name1 { get; set; }

    [SaveState]
    public partial int Index { get; set; }

    [SaveState(Init = "[]")]
    public partial List<string> Values { get; set; }

    public string? Name2 { get; set; }
}

生成的组件属性(分部属性)

using AutoPageStateContainerGenerator;

// <auto-generated/>
#pragma warning disable
#nullable enable
namespace Blazor.Test.Client.Pages;


/// <inheritdoc/>
partial class Counter 
{
    [global::Microsoft.AspNetCore.Components.InjectAttribute]
    public Blazor_Test_Client_Pages_CounterStateContainer StateContainer { get; set; }


    public override string? Type { get => StateContainer.Type; set => StateContainer.Type = value; }


    public partial string? Name1 { get => StateContainer.Name1; set => StateContainer.Name1 = value; }


    public partial int Index { get => StateContainer.Index; set => StateContainer.Index = value; }


    public partial System.Collections.Generic.List<string> Values { get => StateContainer.Values; set => StateContainer.Values = value; }
}

生成的数据容器

using AutoPageStateContainerGenerator;

// <auto-generated/>
#pragma warning disable
#nullable enable
namespace Blazor.Test.Client.Pages;

[AutoPageStateContainerGenerator.GeneratedStateContainerAttribute(Lifetime = 0, Name = "Counter")]
[global::System.CodeDom.Compiler.GeneratedCode("AutoPageStateContainerGenerator.PageStateContainerGenerator", "0.0.4.0")]
/// <inheritdoc/>
public partial class Blazor_Test_Client_Pages_CounterStateContainer : AutoPageStateContainerGenerator.IGeneratedStateContainer, Blazor.Test.Client.Pages.ICounter
{
    public event Action? OnChange;

    private string? type = "CounterParent";

    public string? Type
    {
        get
        {
            return type;
        }
        set
        {
            type = value;
            NotifyStateChanged(); 
        }
    }

    private string? name1;

    public string? Name1
    {
        get
        {
            return name1;
        }
        set
        {
            name1 = value;
            NotifyStateChanged(); 
        }
    }

    private int index;

    public int Index
    {
        get
        {
            return index;
        }
        set
        {
            index = value;
            NotifyStateChanged(); 
        }
    }

    private System.Collections.Generic.List<string> values = [];

    public System.Collections.Generic.List<string> Values
    {
        get
        {
            return values;
        }
        set
        {
            values = value;
            NotifyStateChanged(); 
        }
    }


    private void NotifyStateChanged()
      => OnChange?.Invoke();
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 is compatible.  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 AutoPageStateContainerGenerator:

Package Downloads
BlazorTemplate.Constraints

BlazorWebAdmin模板项目的组件

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.8 233 1/20/2026
0.0.7 216 9/11/2025
0.0.6 207 6/27/2025
0.0.5 344 6/12/2025
0.0.4 256 6/9/2025
0.0.3 176 6/6/2025
0.0.2 204 5/29/2025
0.0.1 198 5/29/2025