Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Blazor Webassembly在UI中显示错误时间_C#_Asp.net Core_Blazor_Blazor Client Side_Blazor Webassembly - Fatal编程技术网

C# Blazor Webassembly在UI中显示错误时间

C# Blazor Webassembly在UI中显示错误时间,c#,asp.net-core,blazor,blazor-client-side,blazor-webassembly,C#,Asp.net Core,Blazor,Blazor Client Side,Blazor Webassembly,我正在.Net 5.0 Blazor WebAssembly中开发我的应用程序。我正在用HTML显示当前时间。但显示的时间始终是系统时钟中显示的时间-5.30小时 在谷歌搜索之后,我在网上发现了以下内容 我在.csproj文件中添加了以下标记,但仍然没有成功 这是我的密码: .csproj: @using System.Threading @implements IDisposable <p>@CurrentDateTime</p> @code { priva

我正在
.Net 5.0 Blazor WebAssembly
中开发我的应用程序。我正在用HTML显示当前时间。但显示的时间始终是系统时钟中显示的时间
-5.30小时

在谷歌搜索之后,我在网上发现了以下内容

我在
.csproj
文件中添加了以下标记,但仍然没有成功

这是我的密码:

.csproj:

@using System.Threading
@implements IDisposable

<p>@CurrentDateTime</p>

@code {
    private Timer _timer;
    public string CurrentDateTime { get; set; }

    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            _timer = new Timer(DateTimeCallback, null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
        }
    }

    private async void DateTimeCallback(object state)
    {
        CurrentDateTime = DateTimeOffset.UtcNow.ToLocalTime().ToString("dd MMM yyyy hh:mm:ss tt");
        await InvokeAsync(StateHasChanged);
    }

    public void Dispose()
    {
        _timer?.Dispose();
    }
}

net5.0
真的
真的
service-worker-assets.js
代码:

@using System.Threading
@implements IDisposable

<p>@CurrentDateTime</p>

@code {
    private Timer _timer;
    public string CurrentDateTime { get; set; }

    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            _timer = new Timer(DateTimeCallback, null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
        }
    }

    private async void DateTimeCallback(object state)
    {
        CurrentDateTime = DateTimeOffset.UtcNow.ToLocalTime().ToString("dd MMM yyyy hh:mm:ss tt");
        await InvokeAsync(StateHasChanged);
    }

    public void Dispose()
    {
        _timer?.Dispose();
    }
}
@使用System.Threading
@实现IDisposable
@当前日期时间

@代码{ 私人定时器; 公共字符串CurrentDateTime{get;set;} 受保护的覆盖无效OnAfterRender(布尔firstRender) { if(firstRender) { _计时器=新计时器(DateTimeCallback,null,TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(1)); } } 私有异步void DateTimeCallback(对象状态) { CurrentDateTime=DateTimeOffset.UtcNow.ToLocalTime().ToString(“dd-MMM-yyy-hh:mm:ss-tt”); 等待调用同步(StateHasChanged); } 公共空间处置() { _计时器?.Dispose(); } }
时间屏幕截图:

@using System.Threading
@implements IDisposable

<p>@CurrentDateTime</p>

@code {
    private Timer _timer;
    public string CurrentDateTime { get; set; }

    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            _timer = new Timer(DateTimeCallback, null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
        }
    }

    private async void DateTimeCallback(object state)
    {
        CurrentDateTime = DateTimeOffset.UtcNow.ToLocalTime().ToString("dd MMM yyyy hh:mm:ss tt");
        await InvokeAsync(StateHasChanged);
    }

    public void Dispose()
    {
        _timer?.Dispose();
    }
}


请帮助解决我缺少的问题。

在更新Microsoft.AspNetCore.Components.WebAssembly
Microsoft.AspNetCore.Components.WebAssembly.DevServer
nuget软件包(从版本
5.0.0
5.0.1

如果没有
ToLocalTime()
,您会得到什么?你的电脑设定了什么时区?肯定是出了问题。它似乎没有将UTC转换回IST(?)
.UtcNow.ToLocalTime()
应该是
。现在
但是这个代码在我的电脑上运行良好。我是UTC+11 atm。@phuzi我的电脑设置为
UTC+5.30
时区。我对
.Now
.Utc.Now
使用或不使用
ToLocalTIme()
和@Brian Parker
,得到了相同的结果。Now
似乎也不起作用不确定,但似乎是一个活的bug