Проницательная страница использует сервис, чтобы увеличить это значение.
Код: Выделить всё
@page "/counter"
@rendermode InteractiveWebAssembly
@inject ICountService CountService
Counter
Counter
Current count: @currentCount
Click me
@code {
private int currentCount = 0;
private void IncrementCount()
{
Console.WriteLine("Incrementing counter with count service...");
currentCount = currentCount + CountService.GetStep();
}
}
< /code>
Counter Service "My Degency" Components: < /p>
public interface ICountService
{
int GetStep();
}
public class VariableCountService : ICountService
{
protected int StepSize { get; set; }
public VariableCountService(int stepSize)
{
StepSize = stepSize;
}
public int GetStep()
{
return StepSize;
}
public override string ToString()
{
return $"Variable Counter [{StepSize}]";
}
}
public class SingleStepCountService : VariableCountService
{
public SingleStepCountService()
: base(1)
{ }
public override string ToString()
{
return $"Single Step Counter";
}
}
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents();
// This WORKS
builder.Services.AddScoped();
// This does NOT
// builder.Services.AddSingleton(new VariableCountService(2));
// This does NOT
//var counter = new VariableCountService(2);
//builder.Services.AddSingleton(counter);
// This does NOT
//var counter = new VariableCountService(2);
//builder.Services.AddSingleton((x) => { return counter; });
var app = builder.Build();
// SNIP
app.Run();
}
}
}
< /code>
Код от зеленой стрелки сработал. Я сделал инкрементные шаги, которые не (желтый), но когда я откатился назад, оригинал больше не работал (кроме инкогнито)
Подробнее здесь: https://stackoverflow.com/questions/797 ... in-incogni
Мобильная версия