我在想这样的事情:
protected override async Task OnInitializedAsync() {
//...
RunMeEndlesslyWithoutAwait();
//...
}
protected async Task RunMeEndlesslyWithoutAwait() {
while (online) {
//... do stuff
await Task.Delay(60000);
}
}
Run Code Online (Sandbox Code Playgroud)
但我不确定这是否是最充分的。
setInterval(...)使用 Blazor WebAssembly 的JS 函数是否有任何已知的最佳/有效方法?
我想在 blazor razor 组件中制作一个完全没有可见边框的表格。我仅用它来订购元素。
例如,带有“border=”0“”的表如下所示(取自 Visual Studio 中 Blazor 模板启动的 Wetaher 服务):
我想删除行之间以及表头和正文之间的线。我尝试过“border =”0“cellpadding =”0“cellspacing =”0“”但它没有任何作用。
我有一个 Blazor 服务器端项目,我一直在 Visual Studio 2019 中使用 .NET5 开发该项目。一切都运转良好。
我刚刚将该站点部署到测试服务器(该服务器上已经运行了两个这样的站点,因此我知道该服务器拥有运行该站点所需的一切),但 Blazor 的东西似乎都不起作用。具体来说...
[Authorize]对任何人都可见不知道要显示什么代码,因为它在 VS 中运行时都有效。App.razor以下是...的内容
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<AuthorizeView>
<Authorized>
<LayoutView Layout="@typeof(MainLayout)">
<NotFound />
</LayoutView>
</Authorized>
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeView>
</NotFound>
</Router>
</CascadingAuthenticationState>
Run Code Online (Sandbox Code Playgroud)
该_Imports.razor文件包含所有正确的using内容,特别包括...
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
Run Code Online (Sandbox Code Playgroud)
非登录用户不应访问的页面[Authorize(Roles = "Admin")]在代码隐藏或@attribute [Authorize(Roles = "Admin")]文件中用 进行修饰 …
所以现在我得到了
Error: System.InvalidOperationException: A second operation was started on this context before a previous operation completed.
Run Code Online (Sandbox Code Playgroud)
因为blazor似乎不尊重当前的请求。
我正在做的是这样的:
FirstComponent.razor
@inject IService _service; // abstracted service that calls EF
<layout> // layout stuff here
<SecondComponent /> // second blazor component
</layout>
@code {
protected override async Task OnInitializeAsync()
{
var getSomething = await _service.OnInitializedAsync();
}
}
Run Code Online (Sandbox Code Playgroud)
SecondComponent.razor
@inject IService _service; // abstracted service that calls EF
@code {
protected override async Task OnInitializedAsync()
{
var getSomething = await _service.GetSomething();
}
} …Run Code Online (Sandbox Code Playgroud) 这是一个具有短过渡的折叠元素的简单示例。(前两个蓝色按钮。)
这是我的 HTML
<p>
<a class="btn btn-primary" @onclick="ToggleDropdown" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
Link with href
</a>
</p>
<div class="collapse @DropdownCssClass" id="collapseExample">
<div class="card card-body">
Anim pariatur cliche...
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是代码
@code {
private bool isDropdownVisible = true;
private string DropdownCssClass => isDropdownVisible ? "collapsing" : "show";
private void ToggleDropdown()
{
isDropdownVisible = !isDropdownVisible;
}
}
Run Code Online (Sandbox Code Playgroud)
div 折叠但没有过渡。如何应用过渡?
谢谢
我想用“附加图标”替换输入文件的矩形。我尝试将图标的 URL 设置为 InputFile 的“背景图像”,但没有效果。 这仅演示了如何更改 InputFile 的颜色,并不完全是我所需要的。
我是 Blazor 的新手,没有太多使用任务的经验,所以希望我只是犯了一个愚蠢的错误。我有一个通过按下按钮调用的异步方法,但如果在 1-2 秒内再次调用该方法,我会收到以下异常。
Error: System.InvalidOperationException: A second operation was started on this context before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
Run Code Online (Sandbox Code Playgroud)
此按钮针对“用户”表中的每一行呈现。我试图快速连续删除多个用户记录,但收到上述错误。
这是按下按钮的代码(使用 AntBlazor)
<Button Type="primary" Danger OnClick="@(async() => await RemoveAsync(user))">Remove User</Button>
Run Code Online (Sandbox Code Playgroud)
这是RemoveAsync 方法的代码。
private async Task RemoveAsync(User user)
{
await UserService.UpdateUserAsync(user);
}
Run Code Online (Sandbox Code Playgroud)
我是否误解了 async/await 的工作原理?或者我是否需要利用任务来确保操作完成?
编辑:
这是 UserService.UpdateUserAsync() 代码
public async Task<bool> UpdateUserAsync(User …Run Code Online (Sandbox Code Playgroud) 我创建了一个应用程序程序集,并引用了 REST-Api 和 Blazor 应用程序。REST-Api 工作正常,但 Blazor 应用程序出现以下错误:
InvalidOperationException:无法从根提供程序解析“MediatR.IRequestHandler`2[Application.Customers.Queries.GetCustomersList.GetCustomersListQuery,Application.Common.Viewmodels.CustomerListVm]”,因为它需要范围服务“Application.Common.Interfaces.IWegisterDbContext”。Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(类型 serviceType、IServiceScope 范围、IServiceScope rootScope)
InvalidOperationException:为 MediatR.IRequestHandler`2[Application.Customers.Queries.GetCustomersList.GetCustomersListQuery,Application.Common.Viewmodels.CustomerListVm] 类型的请求构造处理程序时出错。向容器注册您的处理程序。有关示例,请参阅 GitHub 中的示例。MediatR.Internal.RequestHandlerBase.GetHandler(ServiceFactory工厂)
在下面的代码示例中,我放置了我的 Starup 文件,两个项目都位于同一解决方案中,引用相同的项目。我已经尝试添加授权,但这显然不起作用。我不知道问题是什么,是我遗漏了什么还是这就是 Blazor?两个程序集中的 MediatR 版本相同。
这是我从 REST-Api 启动的:
using Application;
using Application.Common.Interfaces;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Persistence;
using WebApi.Services;
namespace Wegister.WebApi
{
public class Startup
{
public IWebHostEnvironment Environment { get; }
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration, IWebHostEnvironment environment)
{
Configuration = configuration;
Environment = environment;
} …Run Code Online (Sandbox Code Playgroud) 我想在剃刀页面中做这样的事情:
@if (currentwidth<x)
{
le code
}
else
{
le other code
}
Run Code Online (Sandbox Code Playgroud)
我添加了 javascript 文件并将其连接到一个实用程序服务,该服务在我获得静态宽度(我测试过)时起作用。
js:
export function getCurrentWidth() {
return window.addEventListener("resize", () => {
window.innerWidth;
});
}
Run Code Online (Sandbox Code Playgroud)
实用服务方法:
public async Task<double> GetCurrentWidth()
{
var m = await GetModule();
var result = await m.InvokeAsync<double>("getCurrentWidth");
return result;
}
Run Code Online (Sandbox Code Playgroud)
剃刀文件:
double width;
protected async override Task OnInitializedAsync()
{
width = await utilityService.GetCurrentWidth();
}
Run Code Online (Sandbox Code Playgroud)
因此,问题是我无法从 OnInitailzedAsync 调用它,因为该函数仅触发一次,因此我需要一个不断检查 GetCurrentWIdth() 方法以检查调整大小的函数。
在 blazor 中是否有其他方法可以做到这一点,或者我可以使用什么方法/提前谢谢您。
blazor ×10
c# ×5
asp.net-core ×2
css ×2
.net ×1
bootstrap-4 ×1
html-table ×1
javascript ×1
mediatr ×1
mudblazor ×1
razor ×1
setinterval ×1