如何在Blazor中阻止默认行为?

huangapple go评论65阅读模式
英文:

How to prevent default in Blazor?

问题

我在MudBlazor中有一个简单的按钮

```csharp
<MudIconButton Class="answer-button" Icon="@_icon" OnClick="OnClick" Color="@_color" 
               @oncontextmenu="OnContextMenu"
               @oncontextmenu:preventDefault="true"/>

我只是尝试阻止按钮的右键单击操作,并应用自己的逻辑。

我正在使用这篇文章,但我遇到了以下构建错误:

EventCalendarAnswer.razor(5,36): error RZ10010: 组件参数 'oncontextmenu' 在此组件中使用了两次或更多次。参数必须是唯一的(不区分大小写)。组件参数 'oncontextmenu' 是由 '@oncontextmenu:preventDefault' 指令属性生成的。

如何在Blazor中防止默认行为?


<details>
<summary>英文:</summary>

I have this simple button in MudBlazor:

```csharp
&lt;MudIconButton Class=&quot;answer-button&quot; Icon=&quot;@_icon&quot; OnClick=&quot;OnClick&quot; Color=&quot;@_color&quot; 
                   @oncontextmenu=&quot;OnContextMenu&quot;
                   @oncontextmenu:preventDefault=&quot;true&quot;/&gt;

I'm just trying to prevent right click action on button and apply my own logic to it.

I was using this article, but I get this build error

EventCalendarAnswer.razor(5,36): error RZ10010: The component parameter &#39;oncontextmenu&#39; is used two or more times for this component. Paramete
rs must be unique (case-insensitive). The component parameter &#39;oncontextmenu&#39; is generated by the &#39;@oncontextmenu:preventDefault&#39; directive attribute

How can I prevent default behaviour in Blazor?

答案1

得分: 4

你可以将 MudIconButton 包裹在一个 div 内,并将 @oncontextmenu 属性应用于父元素。

<div @oncontextmenu="OnContextMenu" @oncontextmenu:preventDefault="true">
    <MudIconButton Class="answer-button" Icon="@_icon" OnClick="OnClick" Color="@_color" />
</div>
英文:

You can wrap the MudIconButton inside a div and apply the @oncontextmenu attribute to the parent element.

&lt;div @oncontextmenu=&quot;OnContextMenu&quot; @oncontextmenu:preventDefault=&quot;true&quot;&gt;
    &lt;MudIconButton Class=&quot;answer-button&quot; Icon=&quot;@_icon&quot; OnClick=&quot;OnClick&quot; Color=&quot;@_color&quot; /&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年2月6日 03:06:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定