Мой родительский элемент — Home.razor, и он содержит:
Код: Выделить всё
@page "/"
Home
Hello, world!
Welcome to your new app.
Increment:
Increment amount: @_incrementAmount
Odd? @_odd.ToString()
@code {
private int _incrementAmount { get; set; } = 1;
private int _count { get; set; }
private bool _odd;
public string Count
{
get { return _count + "px"; }
set { _count = int.Parse(value); }
}
public async Task OddCountDetected(int count)
{
Console.WriteLine($"OddCountDetected called in Home.razor with count: {count}"); // Debug log
_count = count;
_odd = true;
StateHasChanged(); // Refresh the UI
}
public async Task EvenCountDetected(int count)
{
Console.WriteLine($"EvenCountDetected called in Home.razor with count: {count}"); // Debug log
_odd = false;
StateHasChanged(); // Refresh the UI
}
}
Код: Выделить всё
@page "/counter"
@rendermode InteractiveServer
Counter
Counter
Current count: @currentCount
Click me
@code {
private int currentCount = 0;
[Parameter]
public int IncrementAmount { get; set; } = 1;
[Parameter]
public EventCallback OnOddCount { get; set; }
[Parameter]
public EventCallback OnEvenCount { get; set; }
private async Task IncrementCount()
{
currentCount += IncrementAmount;
Console.WriteLine($"Incrementing: Current count = {currentCount}, IncrementAmount = {IncrementAmount}");
// Check if OnOddCount or OnEvenCount should be invoked
if (int.IsOddInteger(currentCount))
{
Console.WriteLine($"Odd count detected: {currentCount}"); // Log when odd is detected
// Check if there is an assigned delegate
if (OnOddCount.HasDelegate)
{
Console.WriteLine("Invoking OnOddCount callback"); // Log when callback is invoked
await OnOddCount.InvokeAsync(currentCount);
}
else
{
Console.WriteLine("OnOddCount has no delegate");
}
}
else
{
Console.WriteLine($"Even count detected: {currentCount}"); // Log when even is detected
// Check if there is an assigned delegate
if (OnEvenCount.HasDelegate)
{
Console.WriteLine("Invoking OnEvenCount callback"); // Log when callback is invoked
await OnEvenCount.InvokeAsync(currentCount);
}
else
{
Console.WriteLine("OnEvenCount has no delegate");
}
}
}
}
Я пытался измените способ установки делегата, используя следующий код:
Код: Выделить всё
Код: Выделить всё
Параметр Blazor EventCallback не срабатывает
Но даже когда я попробовал что оно все еще не установлено.
Кроме того, еще одним симптомом может быть тот факт, что @_incrementAmount не запускает StateHasChanged() и поэтому не показывает новое значение на странице.< /п>
Подробнее здесь: https://stackoverflow.com/questions/790 ... is-not-set
Мобильная версия