在 Blazor 中使用 @onkeyup

Using @onkeyup in Blazor

提问人:spokoziutek 提问时间:11/9/2023 最后编辑:spokoziutek 更新时间:11/9/2023 访问量:39

问:

因此,我正在 blazor 中制作一个简单的应用,用于转换一些测量值。我有一个输入,应该触发一个函数ConvertUnits()。我希望每次在输入中键入内容时都会触发它,但由于某种原因,它仅在按 Enter 后触发。 每次在输入中输入某个数字时,我应该怎么做才能触发该函数?

这是归因:

<input type="number" class="form-control @(lengthValue <= 0 ? "bg-danger" : "bg-success")" @bind="lengthValue" @oninput="(e => { OnChangeValue(e); })" @onkeyup="(e => { ConvertUnits(); })" />

功能如下:

    private void ConvertUnits()
    {
       result = ConvertLength(lengthValue, selectedLengthUnit, selectedTargetLengthUnit); 
    }

    private double ConvertLength(double value, string fromUnit, string toUnit)
    {
        Dictionary<string, double> lengthFactors = new Dictionary<string, double>
        {
            { "Metry", 1.0 },
            { "Cale", 39.3701 },
            { "Stopy", 3.28084 },
            { "", 0 }
        };

        fromUnit ??= "";
        toUnit ??= "";

        if (lengthFactors.ContainsKey(fromUnit) && lengthFactors.ContainsKey(toUnit))
        {
            return value * lengthFactors[toUnit] / lengthFactors[fromUnit];
        }
        else
        {
            return 0;
        }
    }

我想我尝试了所有事件侦听器,包括@onblur、@onkeydown、@oninput等。

C# HTML Razor Blazor 事件侦听器

评论


答:

0赞 MrC aka Shaun Curtis 11/9/2023 #1

您无需求助于关键事件。

以下是使用正常事件的方法。我冒昧地将转换代码封装到值对象中,从而对其进行了重构。

@page "/"

<PageTitle>Metre Converter</PageTitle>

<h1>Metre Converter</h1>

Welcome to your new app.
<div>
    <label>Enter Value in Metres</label>
    <input type="number" class="form-control mb-3" min="0"
           @bind="lengthValue"
           @bind:after="ConvertUnits"
           @bind:event="oninput" />
</div>

<select class="form-select mb-3" @bind="_selectedUnit" @bind:after="ConvertUnits">
    @if(_selectedUnit is null)
    {
        <option value="" selected disabled> -- Select a Unit -- </option>
    }
    @foreach (var unit in Enum.GetNames(typeof(Length.Units)))
    {
        <option value="@unit">@unit</option>
    }
</select>

<div>
    <pre>Conversion = @_convertedValue</pre>
</div>

@code {
    private double lengthValue;
    private string? _selectedUnit;
    private double? _convertedValue;

    private void ConvertUnits()
    {
        var number = Length.FromMetres(lengthValue);
        _convertedValue = number.GetUnitValue(_selectedUnit);
    }

    public record Length
    {
        private double _length { get; init; }

        private Length(double lengthInMetres)
            => _length = lengthInMetres;

        public double GetUnitValue(string? units)
        {
            if (units is null)
                return this.UnitValue(Units.Metres);

            var u = (Units)Enum.Parse(typeof(Units), units);
            return this.UnitValue(u);
        }

        public double UnitValue(Units units)
            => units switch
            {
                Units.Millimetres => _length * 1000,
                Units.Inches => _length * InchesConversion,
                Units.Feet => _length * FeetConversion,
                _ => _length
            };

        public enum Units
        {
            Metres,
            Inches,
            Feet,
            Millimetres
        }

        public const double FeetConversion = 3.28084;
        public const double InchesConversion = 39.2701;

        public static Length FromMetres(double metres)
            => new(metres);

        public static Length FromFeet(double feet)
            => new(feet / FeetConversion);

        public static Length FromInches(double inches)
            => new(inches / InchesConversion);

    }
}