我认为您可能会错过在 Startup.ConfigureServices 中注册 TestViewModel
您可以访问此链接:https://docs.microsoft.com/en-us/aspnet/core/tutorials/build-your-first-blazor-app?view=aspnetcore-3.0 以了解更多信息。
如果您希望 Blazor 在 IE11 上运行,请添加 Polyfills。访问此链接:https://github.com/Daddoon/Blazor.Polyfill 以了解更多信息。
你可以在这里下载 Polyfills:https://github.com/Daddoon/Blazor.Polyfill/releases
以下是您提出问题后的示例。希望能帮到你,我的朋友:))
1) 模型
namespace BlazorApp.Models
{
public class TestViewModel
{
public string Search { get; set; }
public async void SearchChanged()
{
// Break point set but not hit?
Search = "Hello world";
}
}
}
2) 在启动类中
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>();
services.AddSingleton<Models.TestViewModel>();
}
3)查看
@page "/testview"
@inject Models.TestViewModel VM
<h1>Test Blazor</h1>
<div>
Search:
<input id="search" type="text" @bind="VM.Search" @onkeypress="@VM.SearchChanged" />
<span>@VM.Search</span>
</div>
4) 在 _Host 视图中
@page "/"
@namespace BlazorApp.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>BlazorApp</title>
<base href="~/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
</head>
<body>
<app>
@(await Html.RenderComponentAsync<App>(RenderMode.ServerPrerendered))
</app>
<script src="~/scripts/blazor.polyfill.min.js"></script>
<script src="_framework/blazor.server.js"></script>
</body>
</html>