亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C# 計數器計數到目標數

C# 計數器計數到目標數

不負相思意 2023-09-07 16:34:26
我試圖找出是否有人知道現有的 C# 插件,該插件將以指定的速度計數到目標數字。我希望能夠指定:起始編號結束編號從開始到結束應該花費的時間??梢栽谟嫈灯魍瓿蓵r執行的自定義回調函數。我發現這些插件一個用于 javascript,一個用于 asp.net:https://inorganik.github.io/countUp.js/https://www.nuget.org/packages/Wisej-2-CountUp/我正在使用 asp.net core 和 blazor 制作一個應用程序,我需要一些適合 blazor 的東西,因為它是一項新技術,并且沒有太多關于如何實現 javascript 插件的信息如果他們有任何關于如何在 ASP.NET Core 中實現 countup.js 的示例,他們會對我有很大幫助
查看完整描述

1 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

您可以創建一個可以在多種情況下為您服務的計時器服務:


創建服務類:


public class BlazorTimer

    {

        private Timer _timer;


        internal void SetTimer(double interval)

        {

            _timer = new Timer(interval);

            _timer.Elapsed += NotifyTimerElapsed;

            _timer.Enabled = true;

            _timer.Start();

        }


        private void NotifyTimerElapsed(object sender, ElapsedEventArgs e)

        {

            OnElapsed?.Invoke();

        }


        public event Action OnElapsed;

    }

在 Program.Main 方法中將服務作為瞬態添加到 DI 容器:


builder.Services.AddTransient(config =>

        {

            var blazorTimer = new BlazorTimer();

            blazorTimer.SetTimer(1000);

            return blazorTimer;

        });

用法

@page "/"


@implements IDisposable

@inject BlazorTimer Timer


@count.ToString()



@code{

private int count = 0;


protected override void OnInitialized()

{

    Timer.OnElapsed += NotifyTimerElapsed;


    base.OnInitialized();

}


private void NotifyTimerElapsed()

{

    // Note: WebAssembly Apps are currently supporting a single thread, which 

    // is why you don't have to call

    // the StateHasChanged method from within the InvokeAsync method. But it 

    // is a good practice to do so in consideration of future changes, such as 

    // ability to run WebAssembly Apps in more than one thread.

    InvokeAsync(() => { count++; StateHasChanged(); });

}


public void Dispose()

{

    Timer.OnElapsed -= NotifyTimerElapsed;

}

}


查看完整回答
反對 回復 2023-09-07
  • 1 回答
  • 0 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號