在 Blazor .razor文件中,您可以@typeparam MyType使用泛型参数。例如:
MyComponent.razor
@typeparam MyType
<SomeHtml />
@code
{
[Parameter]
public List<MyType> MyList{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
所以你可以调用:
<MyComponent MyType="MyTypeABC" MyList="@MyData.MyList" />
Run Code Online (Sandbox Code Playgroud)
但我更喜欢后面的代码(razor.cs),我如何使用像@typeparam MyTyperazor.cs 文件中的类型的参数?
我目前的解决方法是:
MyComponent.razor
@inherits MyComponentCode<MyType>
@typeparam MyType
Run Code Online (Sandbox Code Playgroud)
MyComponent.razor.cs
public class MyComponentCode<MyType> : ComponentBase
{
[Parameter]
public List<MyType> MyList{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想念类似的东西[TypeParameter],但也许有更好的解决方案,有什么想法吗?或者也许这是一个关于“如何在后面的代码中使用剃刀@statements”的一般问题。
2020-02-27 更新:
根据Roger Wolf 的建议(见下文),一个更好的方法:
MyComponent.razor
@typeparam MyType
Run Code Online (Sandbox Code Playgroud)
MyComponent.razor.cs
public partial class MyComponent<MyType>
{
[Parameter]
public List<MyType> MyList{ …Run Code Online (Sandbox Code Playgroud) 我有一个带有身份的 Blazor 3.1 应用程序,我想在其中实施 cookie 同意横幅。
在经典的 ASP .NET Core 中,有一个很好的 cookie 同意横幅模板。
@using Microsoft.AspNetCore.Http.Features
@{
var consentFeature = Context.Features.Get<ITrackingConsentFeature>();
var showBanner = !consentFeature?.CanTrack ?? false;
var cookieString = consentFeature?.CreateConsentCookie();
}
@if (showBanner)
{
<div class="container">
<div id="cookieConsent" class="alert alert-info alert-dismissible fade show" role="alert">
Use this space to summarize your privacy and cookie use policy. <a class="alert-link" asp-area="" asp-controller="Home" asp-action="Privacy">Learn More</a>.
<button type="button" class="accept-policy close" data-dismiss="alert" aria-label="Close" data-cookie-string="@cookieString">
<span aria-hidden="true">Accept</span>
</button>
</div>
</div>
<script>
(function () {
var button = …Run Code Online (Sandbox Code Playgroud) 问题看起来很简单,但我还没有找到任何解决方案。我有一个带有onkeydown事件的 Blazor 输入:
<input @onkeydown="@(e => KeyWasPressed(e))"
@onkeydown:preventDefault="@PreventDefault"
id="@GetId()"
name="@GetId()"
@bind-value="@InputValue"
@bind-value:event="oninput" />
Run Code Online (Sandbox Code Playgroud)
用户应该写文本,但用户应该使用箭头键在列表中导航(所以我试图阻止光标移动到文本的顶部和结尾)。
在 JavaScript 中,这可能是这样的:
function KeyWasPressed(e)
{
// key down
if (e.keyCode == 40)
{
e.preventDefault();
// some work...
}
// key up
else if (e.keyCode == 38)
{
e.preventDefault();
// some work...
}
}
Run Code Online (Sandbox Code Playgroud)
如何在 Blazor 中做到这一点?用@onkeydown:preventDefault你可以防止整个输入。如果我将其设置为变量 ( @PreventDefault),则只能阻止下一个输入(因为第一个输入已经发生)。只是为了理解我的意思: