【问题标题】:Blazor Simple Arithmetic ExampleBlazor 简单算术示例
【发布时间】:2020-04-11 19:42:33
【问题描述】:

我做了一个例子来学习 Blazor。例子就是把重量和重心相乘得到力矩。

目标是在重量或重心发生变化时执行计算。 我收到以下错误。

该元素的属性“onchange”被使用了两次或多次。属性必须是唯一的(不区分大小写)。 '@bind' 指令属性使用属性'onchange'。

你能帮我实现目标吗?

 <h3>MomentBasicCalc</h3>

<div class="container">
    <div class="row">
        <div class="col-sm">
            <label for="weight">Weights</label>
            <input type="text" class="form-control" id="weight"
                   @onchange="Calc"  @bind="@Weight" />
        </div>
        <div class="col-sm">
            <label for="CG">CG</label>
            <input type="text" class="form-control" id="CG"
                    @onchange="Calc" @bind=@Cg />
        </div>
        <div class="col-sm">
            <label for="Moment">Moment</label>
            <input type="text" class="form-control" id="Moment" value=@Moment readonly />
        </div>
    </div>
</div>



@code {
    public double Weight { get; set; }
    public double Cg { get; set; } 
    public double Moment { get; set; }

    void Calc()
    {

        Moment = Weight * Cg;

    }
}

【问题讨论】:

    标签: blazor-client-side


    【解决方案1】:

    不支持在单个事件上附加多个事件处理程序。 你应该这样做:

    <div class="container">
            <div class="row">
                <div class="col-sm">
                    <label for="weight">Weights</label>
                    <input type="text" class="form-control" id="weight"
                           **value="@Weight**
                           @onchange="SetWeight"  "/>
                </div>
                <div class="col-sm">
                    <label for="CG">CG</label>
                    <input type="text" class="form-control" id="CG"
                            **value=@Cg**
                            @onchange="SetCG"  />
                </div>
                <div class="col-sm">
                    <label for="Moment">Moment</label>
                    <input type="text" class="form-control" id="Moment" value=@Moment readonly />
                </div>
            </div>
        </div>
    

    @code
    {
        void SetWeight(UIChangeEventArgs e)
            {
                Weight = (string) e.Value;
                Calc();
            }
    
        void SetCG(UIChangeEventArgs e)
            {
                CG = (string) e.Value;
                Calc();
            }
    
    }
    

    【讨论】:

    • 好的,感谢您的建议(传递参数)。在提供的代码中仍然存在一个小错误(未关闭的标签 'div' 没有匹配的结束标签)。今天晚些时候我会看看它。
    • 是的,我的错。我现在已经更正了它,所以它会编译。
    • 你可以在这里找到我的最终解决方案。 github.com/JeanMarcFlamand/CSharpPersonalLearning-Blazor/blob/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2019-06-02
    相关资源
    最近更新 更多