asp.net core - How might I avoid repeat Blazor-generated HTML attributes between conditionally-rendered elements? - Stack Overfl

In a nutshell, how can I make this code better:@if( this.OnClick_Async is not null ) {<div componen

In a nutshell, how can I make this code better:

@if( this.OnClick_Async is not null ) {
    <div component-name="TermDisplay" class="basic"
            @onclick=@(async e => await this.OnClick_Async(e))
            onmouseover="if(@(this.Term.Context is not null) == true && @(this.Term.Context.Context is not null) == true) { window.DisplayMousePopup(event, `@this.Term.Context.Context.Term`); }">
        @this.Term.ToString()
    </div>
} else {
    <div component-name="TermDisplay" class="basic"
            onmouseover="if(@(this.Term.Context is not null) == true && @(this.Term.Context.Context is not null) == true) { window.DisplayMousePopup(event, `@this.Term.Context.Context.Term`); }">
        @this.Term.ToString()
    </div>
}

Because the attributes use Blazor, I'm not sure if I can simply cop out with attribute splatting.

Edit: I realize I may be asking the wrong question for the given code, but the main question remains.

In a nutshell, how can I make this code better:

@if( this.OnClick_Async is not null ) {
    <div component-name="TermDisplay" class="basic"
            @onclick=@(async e => await this.OnClick_Async(e))
            onmouseover="if(@(this.Term.Context is not null) == true && @(this.Term.Context.Context is not null) == true) { window.DisplayMousePopup(event, `@this.Term.Context.Context.Term`); }">
        @this.Term.ToString()
    </div>
} else {
    <div component-name="TermDisplay" class="basic"
            onmouseover="if(@(this.Term.Context is not null) == true && @(this.Term.Context.Context is not null) == true) { window.DisplayMousePopup(event, `@this.Term.Context.Context.Term`); }">
        @this.Term.ToString()
    </div>
}

Because the attributes use Blazor, I'm not sure if I can simply cop out with attribute splatting.

Edit: I realize I may be asking the wrong question for the given code, but the main question remains.

Share Improve this question edited Nov 17, 2024 at 18:51 hamstar asked Nov 17, 2024 at 5:35 hamstarhamstar 3092 silver badges10 bronze badges 7
  • Shorter: @onclick=@(e => this.OnClick_Async(e)), eliding async/await – Henk Holterman Commented Nov 17, 2024 at 8:18
  • Shorter: "if(@(this.Term.Context?.Context is not null) == true)" using ?. – Henk Holterman Commented Nov 17, 2024 at 8:19
  • @HenkHolterman doesn't removing the async specification affect the synchronicity? though i do think using @onclick=this.OnClick_Async ought to work... – hamstar Commented Nov 17, 2024 at 15:30
  • No, it won't change the awaitability. It is a little smaller and faster. Google 'eliding async await' . – Henk Holterman Commented Nov 17, 2024 at 16:11
  • After your update the question is really unclear. Post better code. – Henk Holterman Commented Nov 17, 2024 at 19:16
 |  Show 2 more comments

1 Answer 1

Reset to default 1

On the assumption that OnClick_Async is probably a parameter Func or EventCallback, here's how you can change your specific case.

I simplified the onmouseover to provide a Minimal Reproducible Example. You'll need to add your code back in.

<div component-name="TermDisplay" class="basic"
     @onclick="this.OnButtonClick"
     onmouseover="window.DisplayMousePopup(event, `@_value`);">
    @_value
</div>

@code {
    [Parameter] public Func<MouseEventArgs, Task>? OnClick_Async { get; set; }
    [Parameter] public EventCallback<MouseEventArgs> OnClick { get; set; }

    private string _value = "Hello Blazor";

    private async Task OnButtonClick(MouseEventArgs e)
    {
        // if using a Event
        await this.OnClick.InvokeAsync(e);

        // If using a Func
        if (this.OnClick_Async is not null)
            await this.OnClick_Async.Invoke(e);

        // or if there's nothing to await
        this.OnClick_Async?.Invoke(e);
    }
}

The alternative in more complex situations is to build a RenderFragment using the RenderTreeBuilder. Here's the MS Docs article on the subject:

https://learn.microsoft/en-us/aspnet/core/blazor/advanced-scenarios?view=aspnetcore-8.0

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745637292a4637471.html

相关推荐

  • 20 万 POC,直接拿来用,这不是测试,这是拒绝服务!!!

    之前看到很多人分享 github 上的一个项目,自动收录全网 nuclei 模板文件,目前已经 19 万了,如果直接拿来对目标进行漏洞探测,无疑会对目标造成巨大伤害,意味着可能要对目标发起十九万次请求以上,可以说是一次小型的 DDoS 攻击

    1小时前
    00
  • 长读长测序揭示结直肠癌异常可变剪接图谱与新型治疗靶点

    徐州医科大学肿瘤研究所董东郑骏年教授团队在Genome Medicine杂志发表题为“Long-read sequencing reveals the landscape of aberrant alternative splicing

    1小时前
    00
  • OWASP TOP10

    什么是OWASP?它的全称是 Open Web Application Security Project(开放式 Web 应用程序 安全 项目)TOP 10OWASP Top 10的意思就是10项最严重的Web 应用程序安全风险列表 ,它总

    1小时前
    00
  • Prometheus配置docker采集器

    Prometheus 配置 Docker 采集器Prometheus 是一个开源的监控系统和时间序列数据库,广泛用于容器化环境中。通过监控 Docker 容器,用户可以实时获取服务性能、资源使用情况等信息。本文将介绍如何为 Docker 容

    58分钟前
    00
  • 开源在线考试系统

    看到调问已经开始扩展在线考试的场景,试了一下,发现在线考试的基本能力都已经支持了。主要是考试中的各种计分功能,包括对每道题的选项设置分值计算、考试时间限制等,和官方了解了一下,考试中的其他各项能力也在逐步完善,有需求可以随时

    54分钟前
    00
  • Power BI 无公式实现帕累托图表

    帕累托分析(Pareto Analysis),也被称为8020法则、关键少数法则,是一种常用的管理工具,用于识别和处理影响业务的主要因素。看到李伟坚老师在Excel使用Vega实现了花式帕累托(参考:Excel 零公式实现高级帕累托图表)

    53分钟前
    00
  • Go 语言 Mock 实践

    Mock 是软件测试中的一项关键技术,尤其在单元测试领域,可谓是“顶梁柱”般的存在,几乎不可或缺。它通过模拟真实对象的行为,使我们能在不依赖外部系统的情况下,专注测试代码的核心逻辑。对于测试开发、自动化测试,乃至性能测试中的某些场景,合理使

    51分钟前
    00
  • 深度学习在DOM解析中的应用:自动识别页面关键内容区块

    爬虫代理摘要本文介绍了如何在爬取东方财富吧()财经新闻时,利用深度学习模型对 DOM 树中的内容区块进行自动识别和过滤,并将新闻标题、时间、正文等关键信息分类存储。文章聚焦爬虫整体性能瓶颈,通过指标对比、优化策略、压测数据及改进结果,展示了

    45分钟前
    10
  • UCB的硅光MEMS OCS控制技术

    昨天写的旭创与nEye合作了一个64端口硅光OCS一、光电路交换技术概述(一)电交换与分组交换电路交换的概念源于早期的电话交换机,通过物理连接实现设备间的通信,就像早期打电话时,接线员手动连接线路一样。而分组交换则是

    42分钟前
    00
  • 重装系统只影响C盘吗?深入解析系统重装的全过程

    重装系统只影响C盘吗?深入解析系统重装的全过程 在计算机的日常使用中,重装系统是一个常见的操作,尤其是在系统出现故障、感染病毒或需要优化系统性能时。然而,许多用户对于重装系统的具体过程和影响存在误解,认为重装系统仅仅是对C盘进行清空和重置

    36分钟前
    10
  • maxwell遇到的一则问题

    结论和原因maxwell的元数据库里面没有存储全部的schema数据(就是少数据了),导致相关表的DDL校验失败。PS:我这里maxwell的作用只是采集库表修改情况的统计粗粒度指标,因为之前maxwell在运行报错的时候,直接修改了pos

    32分钟前
    00
  • Windows Server20192022 Evaluation评估版未激活导致关机问题

    摘要&#xff1a;在安装Windows Server 20192022后&#xff0c;会出现系统版本为 Evaluation 评估版情况&#xff0c;如提示Windows许可证已到期&#xff0c;就

    30分钟前
    00
  • 人工智能与ai有什么区别

    一、引言:概念之辨的必要性在科技浪潮席卷全球的当下,人工智能(Artificial Intelligence,简称AI)已成为人们耳熟能详的词汇。然而,当我们深入探讨时,会发现“人工智能”与“AI”这两个表述在语义和使用场景上存在微妙差异。

    30分钟前
    00
  • 最后讲一遍:ChatGPT 快速生成国内外研究现状的方法

    在科研工作中,梳理国内外研究现状有助于明确研究方向,发现研究空白,为后续研究提供理论支持与创新思路。本文将详细介绍如何借助 ChatGPT 高效生成国内外研究现状,帮助您在有限时间内构建全面、专业的文献综述框架,提升学术写作效率与质量。St

    26分钟前
    00
  • 雨晨 22635.5170 Windows 11 企业版 23H2 轻装版

    文件: 雨晨 22635.5170 Windows 11 企业版 23H2 轻装版 install.esd 大小: 2920270404 字节 修改时间: 2025年4月8日, 星期二, 11 : 04 : 59 MD5: D5F8F0AD

    26分钟前
    00
  • Windows系统密钥检测工具PIDKey 2.1中文版

    Windows系统密钥检测工具PIDKey 2.1中文版 【下载地址】Windows系统密钥检测工具PIDKey2.1中文版 Windows系统密钥检测工具PIDKey 2.1中文版是一款功能强大的工具&#xff0c;专为管理Win

    24分钟前
    00
  • 雨晨 26200.5516 Windows 11 IoT 企业版 LTSC 2024 轻装版

    简述&#xff1a;以下为YCDISM (雨晨作品自2025年03月25日起通用介绍&#xff0c;若无重大更改不再额外敖述) 全程由最新YCDISM2025脱机装载26100.1742_zh-cn_windows_11_

    20分钟前
    00
  • 用Xshell8配置密钥登陆

    1.首先在服务端查看root.sshauthorized_keys是否存在,这是存储公钥的文件,若不存在需新建此文件 2. 打开Xshell8,选择"新建",选择"新建用户密钥生成向导" 给用户

    16分钟前
    00
  • 设计模式:工厂方法模式(Factory Method)(2)

    当年做一个项目时,还不懂什么是设计模式,仅仅是按照经验完成了需求。回头看看,就是暗合桥接模式。但是,在整个需求实现过程中,甲方需要我在已经设计好的标准业务逻辑中添加非标的需求,因为,在他们眼里,从业务角度来看,是自然的拓展。如果当年我知道还

    11分钟前
    00
  • 连Claude 3.5都败下阵来,大语言模型能否定位软件服务的故障根因?

    论文的第一作者是香港中文大学(深圳)数据科学学院三年级博士生徐俊杰龙,指导老师为香港中文大学(深圳)数据科学学院的贺品嘉教授和微软主管研究员何世林博士。贺品嘉老师团队的研究重点是软件工程、LLM for DevOps、大模型安全。大型语言模

    28秒前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信