使用等价的Javascript SetTimeOut()函数在Blazor .net上做动画。

问题描述 投票:0回答:1

问题:在过去,我可以使用Javascript中的SetimeOut函数来制作动画。在过去,我已经能够使用Javascript中的SetimeOut函数来制作动画,这是一个球落下的代码。

var ball = <HTMLElement>document.getElementById("ball");
ball.setAttribute("style", "fill:blue");

var inc2: number = 0;
var timet = document.getElementById("timeT");
timet.setAttribute("y", "420");
//time=200 ms // 1m is 3780 pixel // 400 pixels are 2m / ball is falling from 2m // g=10m/s
fallt2();
var timet2 = document.getElementById("timeT2");
timet2.setAttribute("y", "440");
var tiem = new Date();
var mili = tiem.getMilliseconds();
var dated = Date.now();
function fallt2() {
    //while (inc2 <= 400) {
    let ii = 0; //200 ms in 8 then 25 ms // 400 px high
    //let slow = 3;

    for (; inc2 <= 400; inc2 += 2)//px //10
    {

        goodST2(inc2, ii);
        ii += 25;
    }
}

function goodST2(i: number, ii: number) {
    setTimeout(() => {
        ball.setAttribute("transform", `translate(0 ${i})`);
        var newdate = Date.now();
        var newdiff = newdate - dated;
        mili += 25;
        timet.innerHTML = mili.toString();

        timet2.innerHTML = newdiff.toString();
    }, ii);
}

所以请帮助我一个.net标准函数,如Theading.sleep在Blazor中做动画。 谢谢。

这是我试过的。

@*<svg width="12cm" height="4cm" viewBox="0 0 1200 400">*@
    @*<button class="btn btn-primary" @onclick="transformar">Ver caer</button>*@
    <button class="btn btn-primary" @onclick="tt">Ver caer</button>
    <div>
        <svg viewBox="0 0 100 100">
            @*<circle cx="10" cy="@c1" r="5" fill="blue" stroke="black" stroke-width="0.2" transform="translate(10,@c1)">*@
            <circle cx="10" cy="8" r="5" fill="blue" stroke="black" stroke-width="0.2" transform="translate(10,@c1)">
                @*<animate attributeName="cy" attributeType="XML" begin="0s" dur="4s" from="8" to="28" fill="remove" />*@
            </circle>

            <rect x="10" y="25" width="4" height="4" fill="red" stroke="black" stroke-width="0.5">
                @*<animate attributeName="rx" values="0;5;0" dur="10s" repeatCount="indefinite"></animate>*@
            </rect>
        </svg>
    </div>
</div>
@{tt(); }
    @code {
        private void transformar()
        {
            c1++;
        }
        // Demonstrates how a parent component can supply parameters
        [Parameter]
        public string Saludos { get; set; }
        string Saludos2 = "Hola Diego";
        public bool bA { get; set; }
        [Parameter] public  EventCallback<bool> TransitionEnded { get; set; }
        int c1 = 8;

        void tt()
        {
            System.Timers.Timer t = new System.Timers.Timer();
            t.Interval = 500;
            t.AutoReset = true;
            t.Enabled = true;
            t.Start();
            for (var i = 0; i < 16; i++)
            {
                t.Elapsed += ett;

            }
            t.Stop();
        }

        public void ett(Object source, System.Timers.ElapsedEventArgs e)
        {
            caer2();
        }

        void caer2()
        { c1++; }
        //SetTimer(c1);
        //public int c1 { get; set; } = 8;
        //for(var i=10;i<22;i++>)
        //    {
        //    }
        private static System.Timers.Timer aTimer;
        private static void SetTimer(int c1)
        {
            aTimer = new System.Timers.Timer(500);
            //aTimer.Elapsed += caer(c1); //OntimedEvent;
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
        }
        //private static void caer(Object source, System.Timers.ElapsedEventArgs e, int s)
        private void caer()
        {
            int dormir = 0;
            for (var i=0;i<16;i++)
            {
                c1++;
                //SurveyPrompt2.c1 c1++;
                //System.Timers.Timer(500);
                //Task.Run(()=>CaerAnimate());
                //System.Threading.Thread.Sleep(200);
                //esperar(dormir+200);
                //Task.WaitAll(Task.Delay(100));

            }
        }
        void esperar(int d)
        {
            System.Threading.Thread.Sleep(d);
        }

        async Task CaerAnimate()
        {
            bA = !bA;
            await Task.Delay(10600);
            await TransitionEnded.InvokeAsync(bA);
            //var tcs = new TaskCompletionSource<bool>();
            //DateTime endT = DateTime.Now.AddMilliseconds(3000);
            //while (DateTime.Now<endT)
            //{
            //await Task.Delay(10000000);
            //c1++;
            //}
            //return tcs.Task;
            //await System.Threading.Thread.Sleep(100);

        }


    }

我尝试了定时器,但没有工作,或者我不知道如何实现。先谢谢你了。

animation settimeout sleep blazor .net-standard
1个回答
1
投票

我删除了所有多余的代码,以保持它的简洁。

<button class="btn btn-primary" @onclick="tt">Ver caer</button>
<div>
    <svg viewBox="0 0 100 100">
        <circle cx="10" cy="8" transform="translate(10,@c1)" r="5" fill="blue" stroke="black" stroke-width="0.2" >
        </circle>

        <rect x="10" y="25" width="4" height="4" fill="red" stroke="black" stroke-width="0.5">
        </rect>
    </svg>
</div>
@code {
    int c1 = 8;

    private static System.Timers.Timer aTimer;
    void tt()
    {
        if (aTimer is null)
        {
            aTimer = new System.Timers.Timer(500);
            aTimer.Elapsed += (sender,args) => { c1 = c1 > 16 ? 8 : c1+1; InvokeAsync(StateHasChanged); };
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
        }
        else
        {
            aTimer.Stop();
            aTimer.Dispose();
            aTimer = null;
        }
    }

}

你可以使用定时器,但在? Elapsed 事件,你需要调用 StateHasChanged 来标记要渲染的组件--你应该通过 InvokeAsync 以确保它发生在Renderers上下文中。

在这里看到它的行动。https:/blazorfiddle.coms9jbd8f34

© www.soinside.com 2019 - 2024. All rights reserved.