【发布时间】:2020-03-21 21:21:36
【问题描述】:
我有一个运行良好的 Blazor(服务器)应用程序,它遵守 Microsoft.CodeAnalysis.FxCopAnalyzers 和 StyleCop.Analyzers 设置的所有规则。
一个大幅缩减的剃须刀页面如下:
@inherits OwningComponentBase<MyService>
@inject IModalService ModalService
@inject IJSRuntime JSRuntime
// UI code
@code
{
private readonly CancellationTokenSource TokenSource = new CancellationTokenSource();
ElementReference myElementReferenceName;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await this.myElementReferenceName.FocusAsync(this.JSRuntime);
}
protected override async Task OnInitializedAsync()
{
....
}
public void Dispose()
{
this.TokenSource.Cancel();
}
protected void ShowModalEdit(object someObject)
{
.....
Modal.Show<MyPage>("Edit", parameters);
}
}
注意#1:我使用基于Daniel Roth's suggestion的@inherits OwningComponentBase<MyService>
注意#2:我正在使用Chris Sainty's Modal component 组件
但是,当我尝试将所有代码从 @code {...} 部分移动到“代码隐藏”部分类(“MyPage.razor.cs”)时,我遇到了以下错误......
“我的页面”不包含“服务”的定义,并且无法访问 扩展方法“服务”接受.....
'MyPage.OnAfterRenderAsync(bool)': 找不到合适的方法来覆盖
'MyPage.OnInitializedAsync()': 找不到合适的方法来覆盖
类型“MyPage”不能用作泛型中的类型参数“T” 类型或方法 'IModalService.Show(string, ModalParameters, 模态选项)'。没有隐式引用转换 'MyPage' 到 'Microsoft.AspNetCore.Components.ComponentBase'。
建议?
【问题讨论】:
-
显示(大纲)CodeBehind 类。
-
部分课程是only available in Core 3.1,你用的是什么版本?
-
我正在使用 Core 3.1 最新预览版
-
从 ComponentBase 继承在后面的代码中就足够了
标签: code-behind blazor blazor-server-side