Blazor 在不同文化中更改输入数值

问题描述 投票:0回答:1
input localization blazor cultureinfo
1个回答
1
投票

由于您没有提供足够的代码或上下文,这里有一个显示更新工作的演示页面:

@page "/"

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

<div class="m-2">
    <h4>Net6.0+</h4>
    @foreach (var model in _models)
    {
        <label class="form-label small">@model.Country</label>
        <input type="number" step="0.1" class="form-control mb-3"
               value="@model.Value"
           @onchange="(e) => On6Change(e, model)" />
    }
</div>

<div class="m-2">
    <h4>Net7.0+</h4>
    @foreach (var model in _models)
    {
        <label class="form-label small">@model.Country</label>
        <input type="number" step="0.1" class="form-control mb-3"
           @bind:get=model.Value
           @bind:set="(value) => On7Change(value, model)" />
    }
</div>

<div class="bg-dark text-white">
    @foreach (var model in _models)
    {
        <pre>@model.Country Population : @model.Value</pre>
    }
</div>

@code {
    private List<Model> _models = new()
    {
        new(){ Country="France", Value = 46.1 },
        new(){ Country="Spain", Value = 67.4 },
    };

    private async Task On6Change(ChangeEventArgs e, Model country)
    {
        // Fake doing some async work
        await Task.Yield();
        country.Value = double.Parse((e.Value?.ToString() ?? "0"), System.Globalization.CultureInfo.InvariantCulture);
    }

    private async Task On7Change(double value, Model country)
    {
        // Fake doing some async work
        await Task.Yield();
        country.Value = value;
    }

    public class Model
    {
        public string Country { get; set; } = string.Empty;
        public double Value { get; set; }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.