我添加了一个定期执行某些操作的后台服务,例如官方示例。
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddHostedService<TimedHostedService>(); <-- here
services.AddSingleton<WeatherForecastService>();
}
Run Code Online (Sandbox Code Playgroud)
TimedHostedService有和。 最终,我想在网络浏览器中调用这些。StartAsyncStopAsync
在FetchData.razor默认脚手架的文件中,我尝试直接引用该服务,但这不起作用。因此,我添加了Start和Stop方法WeatherForecastService并在单击事件上调用它们。
<button @onclick="()=> { ForecastService.Stop(); }">Stop</button>
Run Code Online (Sandbox Code Playgroud)
现在的问题是,我不知道如何TimedHostedService在Stop的方法中获取 的运行实例WeatherForecastService。
public class WeatherForecastService
{
....
public void Stop()
{
//how to get TimedHostedService instance?
}
....
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用依赖注入来获取服务提供者,但GetService返回 null。
IServiceProvider sp;
public WeatherForecastService(IServiceProvider sp)
{
this.sp = sp;
}
public void Stop()
{
var ts = sp.GetService(typeof(TimedHostedService)) as …Run Code Online (Sandbox Code Playgroud) Microsoft(Daniel Roth 和 Luke Latham)撰写的ASP.NET Core Blazor 简介文章展示了 Razor 代码中等待调用的示例,例如
@code {
private WeatherForecast[] forecasts;
protected override async Task OnInitializedAsync()
{
forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,没有一个示例表明剃刀页面是否应该在捕获的上下文上继续,例如
.ConfigureAwait(false);
or
.ConfigureAwait(true);
Run Code Online (Sandbox Code Playgroud)
Blazor 是否有 UI 线程是唯一能够更新组件的线程的概念?在页面和/或页面组件中调用等待的调用时,什么被认为是“最佳实践”。
对于普通的 MVC 风格网站,我获得了一组可以自定义的 ASP.NET Core Identity 预生成页面。有了它,我可以做一些事情,比如在注册页面上询问用户的年龄。
服务器端 Blazor 的等效项是什么?
我有这个输入来捕获电话号码。当用户输入数字并按“Enter”键时,会触发“KeyWasPressed”方法并进行一些验证。这按预期工作,但是......
例如,当用户从 Excel 复制并粘贴数字时,变量@Phone不会更新其值,因此当用户按“Enter”键时,验证会发送空值。
@Phone当某些文本粘贴到输入控件时,有没有办法刷新/更新变量?
这是我的代码片段:
<input type="number" class="form-control" @bind="@Phone" @onkeypress="@(async e => await KeyWasPressed(e))" placeholder="Client Phone Number" />
@code {
string Phone { get; set; }
private async Task GetClientInfo()
{
if(String.IsNullOrWhiteSpace(Phone))
{
notificationMessages = $"Add a phone number";
}
else
{
showSpinner = true;
clientInfo = await ApiHelper.GetClientInfoByPhone(Phone);
if(clientInfo != null)
{
var singleViewId = clientInfo?.SingleViewId;
var customerNumber = clientInfo?.Accounts?.FirstOrDefault().CustomerNumber;
var status = clientInfo?.Accounts?.FirstOrDefault().Status;
showClientInformation = true;
var CrossSell = clientInfo?.Accounts[0]?.CrossSell; …Run Code Online (Sandbox Code Playgroud) 我有第二个页面接收以下参数:
@page "/newrecord/{Employees}"
@inject HttpClient http
<h1>@Employees.Count</h1>
@code {
[Parameter]
public List<Model.Employees> Employees{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的主页上,当单击如下按钮时,我会传递列表:
List<Model.Employees> selected { get; set; }
private void OnAddRecord()
{
NavigationManager.NavigateTo($"newrecord/{this.selected}");
}
Run Code Online (Sandbox Code Playgroud)
单击按钮时出现错误,我可以看到 URL 的格式如下:
如何将该对象传递到第二页?我是否必须使用 LocalStorage?或者使用其他类型的对象?
我正在尝试为我的 blazor 应用程序创建一个代码隐藏文件(使用 .NET Core 3.1,Blazor 服务器端),同时它也会在解决方案资源管理器中显示为 razor 文件,您可以单击“打开”,然后查看树形结构中的 razor.cs 文件。
我设法使功能正常工作,但它仍然没有在解决方案资源管理器中正确显示。
据我所知,我做的一切都是正确的,我什至尝试使用项目模板,但这也不起作用。无论我尝试什么,它们总是显示为单独的文件。
文件名是相同的,除了扩展名之外,该类继承自ComponentBase,我尝试过在文件FetchData或FetchDataBase中调用该类,都不起作用。
我的类文件名为:“FetchData.razor.cs” 我的剃刀文件名为“FetchData.razor”
我错过了什么吗?我使用的是 VS2019 V16.5.2,我需要使用预览版吗?
代码片段: 类:
public partial class FetchDataBase : ComponentBase
{
[Inject]
private CoordinatesService CoordinatesService { get; set; }
protected IEnumerable<ICoordinate> coordinates;
protected override async Task OnInitializedAsync()
{
coordinates = await CoordinatesService.GetCoordinates();
}
}
Run Code Online (Sandbox Code Playgroud)
剃须刀页面:
@page "/fetchdata"
@inherits FetchDataBase
<h1>Coordinates</h1>
<p>This component demonstrates fetching data from a service.</p>
@if (coordinates == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Test</th>
</tr> …Run Code Online (Sandbox Code Playgroud) 我使用Blazorise DataGrid组件来显示我的主/详细数据,您在其中单击主行,DataGrid 显示详细行/行。
如何使用RowDoubleClickedDataGrid 的功能并更改其自然行为,以在事件触发时显示详细信息行?
是否有办法保持双向绑定,并在 @onchange 上调用方法?
像这样的东西。
<input class="form-control" placeholder="My Number" @bind="Record.Number" @onchange="(()=> MyMethod())">
Run Code Online (Sandbox Code Playgroud)
我希望它绑定,但在更改时调用一些其他方法,该方法不会从更改事件中设置 Record.Number 值。
@onkeyup 有效,但我想确保该值实际发生变化。
我有一个包含 Blazor Web 程序集和使用个人用户帐户(身份验证)托管的 Asp.Net Core 的项目。但我有两个问题。首先我想要自定义注册并登录查看(与身份服务器4相关),但我找不到视图。在服务器项目(blazor server)中_LoginPartial.cshtml查看,我们可以看到很多页面:、asp-area="Identity" asp-page="/Account/Manage/Index"等等...但是Identity文件夹是空的!同样在 Blazor 客户端中我们有页面:asp-area="Identity" asp-page="/Account/Logout"asp-area="Identity" asp-page="/Account/Register"LoginDisplay.razor
<NotAuthorized>
<a href="authentication/register">Register</a>
<a href="authentication/login">Log in</a>
</NotAuthorized>
Run Code Online (Sandbox Code Playgroud)
在 Pages 文件夹中我们有Authentication.razorComponent :
@page "/authentication/{action}"
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
<RemoteAuthenticatorView Action="@Action" />
@code{
[Parameter] public string Action { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
Authentication.razor组件间接调用 IdentityServer4。
如何找到 Identity Server 4 视图并更改它?
第二个问题是当我想在启动文件 blazor 服务器中使用自定义身份模型时。
默认代码是:
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)
当我更改为:
services.AddIdentity<ApplicationUser,Role>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)
当我运行项目并单击登录或注册按钮(Authentication.razor)调用 IdentityServer(例如https://localhost:5001/Identity/Account/Register?returnUrl=%2Fauthentication%2Flogin …
identityserver4 blazor-server-side blazor-client-side blazor-webassembly
当我在 Visual Studio 中打开 Blazor 解决方案时,Intellisense 将显示对 Blazor 组件的引用的错误错误,并将成功编译。
例如,在我的许多类中,我注入了ILogger<BlazorComponent>一个需要记录的组件,在本例中是我的Details.razor组件:
public partial class DetailsBase : ComponentBase
{
[Inject]
protected ILogger<Details> Logger { get; set; }
// etc etc...
}
Run Code Online (Sandbox Code Playgroud)
但是,Intellisense 始终显示 未找到 的错误Details。它将成功编译,但虚假错误将保留,直到您导航到该特定文件(Details.razor在本例中)并且 Intellisense 拾取它。
这些虚假错误使我的调试窗口变得混乱,我想停止它们,而不必不断单击以转到文件以使其被 Intellisense 识别。
我可以启用某种设置来确保 Intellisense 检查这些文件吗?或者有什么东西比 Intellisense 更适合 Blazor?
blazor ×8
c# ×3
asp.net-core ×2
.net-core ×1
async-await ×1
blazorise ×1
datagrid ×1
intellisense ×1
keypress ×1