C# Blazor WebAssembly:参数 2:无法从“void”转换为“Microsoft.AspNetCore.Components.EventCallback”

Posted

技术标签:

【中文标题】C# Blazor WebAssembly:参数 2:无法从“void”转换为“Microsoft.AspNetCore.Components.EventCallback”【英文标题】:C# Blazor WebAssembly: Argument 2: cannot convert from 'void' to 'Microsoft.AspNetCore.Components.EventCallback' 【发布时间】:2021-03-05 20:06:35 【问题描述】:

我是 blazor C# 的新手,正在尝试制作一个简单的倒数计时器网站。我的网站包括:

显示计时器的文本 启动和停止按钮 设置定时器的按钮

设置计时器的按钮有问题。当我点击它时,它不会设置计时器显示并且我收到错误Argument 2: cannot convert from 'void' to '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>

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);
        
    

【问题讨论】:

【参考方案1】:

试试:&lt;button @onclick="() =&gt; SetTimer(timer.Id)"&gt;

【讨论】:

这与您正在循环的事实有关,因此会创建多个回调。我将需要再次阅读此(telerik.com/blogs/…),但我认为它包含您的解释;-) onclick 接受 0 个参数或 1 个 MouseEventArgs 类型的参数。您无法添加自己的,因此您需要使用匿名函数来使用捕获的变量调用您的实际方法。

以上是关于C# Blazor WebAssembly:参数 2:无法从“void”转换为“Microsoft.AspNetCore.Components.EventCallback”的主要内容,如果未能解决你的问题,请参考以下文章

不用Blazor WebAssembly,开发在浏览器端编译和运行C#代码的网站

使用 C#(无 JavaScript)直接将 Blazor WebAssembly 文件分块到 Azure Blob 存储

将现有 C# 代码编译为 WebAssembly

Blazor WebAssembly 3.2 正式发布

如何从 Blazor WebAssembly 中的 Razor 页面访问 body 标签或其他标签?

Blazor 性能