AutoPageStateContainerGenerator 0.0.2
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package AutoPageStateContainerGenerator --version 0.0.2
NuGet\Install-Package AutoPageStateContainerGenerator -Version 0.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="AutoPageStateContainerGenerator" Version="0.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AutoPageStateContainerGenerator" Version="0.0.2" />
<PackageReference Include="AutoPageStateContainerGenerator" />
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.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AutoPageStateContainerGenerator, 0.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.
#:package AutoPageStateContainerGenerator@0.0.2
#: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.2
#tool nuget:?package=AutoPageStateContainerGenerator&version=0.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
AutoPageStateContainerGenerator
介绍
Blazor的组件数据(或者说组件的属性),如果没有做特殊处理的话,在路由切换的时候就会丢失当前页面上的数据,不管是Server还是WebAssembly,官方文档的推荐做法都是将数据抽出来当作一个服务,在页面中注入使用(内存中状态容器服务),而使用生成器,就可以很方便的完成这些功能,自动为Razor组件生成相应的数据容器。
使用
StateContainerAttribute
在需要生成数据容器的组件上标注。
SaveStateAttribute
标注需要保存的字段。
AddStateContainers
注入生成的数据容器
父类中使用
必须使用virtual属性
public partial class CounterParent : ComponentBase
{
[SaveState]
public virtual string? Type { get; set; } = "CounterParent";
public void CheckType()
{
Console.WriteLine(Type);
}
}
示例
Counter组件,不能直接在razor文件中添加该Attribute。
使用字段
[StateContainer]
public partial class Counter
{
[SaveState]
private string? name1;
[SaveState]
private int index;
[SaveState]
private List<string> values = [];
public string? Name2 { get; set; }
}
使用分部属性
[StateContainer]
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 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]
[global::System.CodeDom.Compiler.GeneratedCode("AutoPageStateContainerGenerator.PageStateContainerGenerator", "0.0.1.0")]
/// <inheritdoc/>
public partial class Blazor_Test_Client_Pages_CounterStateContainer : AutoPageStateContainerGenerator.IGeneratedStateContainer
{
public event Action? OnChange;
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 | Versions 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 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.
-
net6.0
-
net8.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.