C#Blazor WebAssembly:参数2:无法从';无效';至';Microsoft.AspNetCore.Components.EventCallback';
我是blazor C#的新手,正在尝试制作一个简单的倒计时网站。我的网站包括:C#Blazor WebAssembly:参数2:无法从';无效';至';Microsoft.AspNetCore.Components.EventCallback';,c#,blazor,blazor-webassembly,C#,Blazor,Blazor Webassembly,我是blazor C#的新手,正在尝试制作一个简单的倒计时网站。我的网站包括: 显示计时器的文本 启动和停止按钮 设置计时器的按钮 我设置计时器的按钮有问题。当我单击它时,它不会设置计时器显示,我得到一个错误参数2:无法从“void”转换为“Microsoft.AspNetCore.Components.EventCallback”。我在Youtube上搜索EventCallback主题,但问题是,我的组件没有分离,而在视频中,示例代码将分离的组件链接在一起。这是代码。 Index.razo
- 显示计时器的文本
- 启动和停止按钮
- 设置计时器的按钮
参数2:无法从“void”转换为“Microsoft.AspNetCore.Components.EventCallback”
。我在Youtube上搜索EventCallback主题,但问题是,我的组件没有分离,而在视频中,示例代码将分离的组件链接在一起。这是代码。Index.razor
@page "/"
@inherits FrontEnd.Components.Timer
<h1>Timer</h1>
<p class="timer">@Hours.ToString("00"):@Minutes.ToString("00"):@Seconds.ToString("00")</p>
@foreach (var timer in timerCollections)
{
// Here's the problem
<button @onclick="SetTimer(timer.Id)">@timer.Hours.ToString("00"):@timer.Minutes.ToString("00"):@timer.Seconds.ToString("00")</button>
}
<br/>
<button @onclick="StartTimer" disabled=@StartButtonIsDisabled>Start</button>
<button @onclick="StopTimer" disabled=@StopButtonIsDisabled>Stop</button>
@page/“
@继承FrontEnd.Components.Timer
计时器
@Hours.ToString(“00”):@Minutes.ToString(“00”):@Seconds.ToString(“00”)
@foreach(timerCollections中的var计时器)
{
//问题出在这里
@timer.Hours.ToString(“00”):@timer.Minutes.ToString(“00”):@timer.Seconds.ToString(“00”)
}
开始
停止
Timer.cs
using System;
using System.Timers;
using System.Collections.Generic;
using Microsoft.AspNetCore.Components;
namespace FrontEnd.Components
{
public class TimerStructure
{
public int Id { get; set; }
public int Hours { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
}
public class Timer : ComponentBase
{
public List<TimerStructure> timerCollections = new List<TimerStructure>(){
new TimerStructure(){ Id = 1, Hours = 0, Minutes = 30, Seconds = 0 },
new TimerStructure(){ Id = 2, Hours = 1, Minutes = 0, Seconds = 0 }
};
public int Index { get; private set; }
public int Hours { get; set; } = 0;
public int Minutes { get; set; } = 0;
public int Seconds { get; set; } = 0;
public bool StopButtonIsDisabled { get; set; } = true;
public bool StartButtonIsDisabled { get; set; } = false;
private static System.Timers.Timer aTimer;
// and this is the function related to the problem
public void SetTimer(int value)
{
this.Index = value - 1;
Hours = timerCollections[Index].Hours;
Minutes = timerCollections[Index].Minutes;
Seconds = timerCollections[Index].Seconds;
}
public void StopTimer()
{
aTimer.Stop();
aTimer.Dispose();
StopButtonIsDisabled = true;
StartButtonIsDisabled = false;
Console.WriteLine($"{Hours}:{Minutes}:{Seconds}");
}
public void StartTimer()
{
aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += CountDownTimer;
aTimer.Start();
StopButtonIsDisabled = false;
StartButtonIsDisabled = true;
}
public void CountDownTimer(Object source, ElapsedEventArgs e)
{
if(Seconds == 0 && Minutes > 0)
{
Minutes -= 1;
Seconds = 59;
} else if (Minutes == 0 && Seconds == 0 && Hours > 0)
{
Hours -= 1;
Minutes = 59;
Seconds = 59;
} else if (Hours == 0 && Minutes == 0 && Seconds == 0)
{
aTimer.Stop();
aTimer.Dispose();
StopButtonIsDisabled = true;
StartButtonIsDisabled = false;
} else
{
Seconds -= 1;
}
InvokeAsync(StateHasChanged);
}
}
}
使用系统;
使用系统计时器;
使用System.Collections.Generic;
使用Microsoft.AspNetCore.Components;
名称空间前端组件
{
公共类计时器结构
{
公共int Id{get;set;}
公共整数小时数{get;set;}
公共整数分钟数{get;set;}
公共整数秒{get;set;}
}
公共类计时器:ComponentBase
{
公共列表timerCollections=新列表(){
新的TimerStructure(){Id=1,小时=0,分钟=30,秒=0},
新的TimerStructure(){Id=2,小时=1,分钟=0,秒=0}
};
公共int索引{get;private set;}
公共整数小时数{get;set;}=0;
公共整数分钟数{get;set;}=0;
公共整数秒{get;set;}=0;
public bool StopButtonIsDisabled{get;set;}=true;
公共布尔开始按钮禁用{get;set;}=false;
专用静态系统定时器定时器定时器;
//这就是与问题相关的函数
公共无效设置计时器(int值)
{
该指数=值-1;
小时=时间集合[索引]。小时;
分钟=timerCollections[索引]。分钟;
秒=timerCollections[索引]。秒;
}
公共无效停止计时器()
{
停止();
aTimer.Dispose();
StopButtonIsDisabled=真;
StartButtonIsDisabled=false;
WriteLine($“{Hours}:{Minutes}:{Seconds}”);
}
公共无效StartTimer()
{
aTimer=新系统计时器计时器(1000);
aTimer.Appead+=倒计时;
aTimer.Start();
StopButtonIsDisabled=假;
StartButtonIsDisabled=真;
}
公共无效倒计时(对象源,ElapsedEventArgs e)
{
如果(秒==0&&Minutes>0)
{
分钟-=1;
秒=59;
}否则如果(分钟==0&&Seconds==0&&Hours>0)
{
小时数-=1;
分钟=59;
秒=59;
}如果(小时==0&&Minutes==0&&Seconds==0),则为else
{
停止();
aTimer.Dispose();
StopButtonIsDisabled=真;
StartButtonIsDisabled=false;
}否则
{
秒-=1;
}
InvokeAsync(statehaschange);
}
}
}
试试看: