gpt4 book ai didi

code-behind - Blazor 服务器 - 'Code Behind' 模式 : OnInitializedAsync(): no suitable method found to override

转载 作者:行者123 更新时间:2023-12-03 16:56:54 30 4
gpt4 key购买 nike

我有一个运行良好的 Blazor(服务器)应用程序,它遵守 Microsoft.CodeAnalysis.FxCopAnalyzers 设置的所有规则和 StyleCop.Analyzers .
一个大幅缩减的 Razor 页面如下:

@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:我使用了 @inherits OwningComponentBase<MyService>基于 Daniel Roth's suggestion
注意#2:我使用的是 Chris Sainty's Modal component零件
但是,当我尝试从 @code {...} 中移动所有代码时部分到“代码背后”部分类(“MyPage.razor.cs”),然后我遇到以下错误......

'MyPage' does not contain a definition for 'Service' and no accessibleextension method 'Service' accepting .....

'MyPage.OnAfterRenderAsync(bool)': no suitable method found to override

'MyPage.OnInitializedAsync()': no suitable method found to override

The type 'MyPage' cannot be used as type parameter 'T' in the generictype or method 'IModalService.Show(string, ModalParameters,ModalOptions)'. There is no implicit reference conversion from'MyPage' to 'Microsoft.AspNetCore.Components.ComponentBase'.


建议?

最佳答案

您的 MyPage.razor.cs应该继承自 ComponentBase类(class)和你的Mypage.razor应该继承自 MyPage.razor.cs .
在您的“代码隐藏”类中,您应该使用 [Inject]您注入(inject)的每项服务的属性,并使其至少为 protected属性以便能够在 Razor 组件中使用它们。
下面是我的一个测试应用程序的示例,请注意这使用 .net-core 3.0,在 3.1 中您可以使用部分类。
Index.razor

@page "/"
@inherits IndexViewModel

<div class="row">
<div class="col-md">

@if (users == null)
{
<p><em>Hang on while we are getting data...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th class="text-danger">Id</th>
<th class="text-danger">Username</th>
<th class="text-danger">Email</th>
<th class="text-danger">FirstName</th>
<th class="text-danger">LastName</th>
</tr>
</thead>
<tbody>
@foreach (var user in users)
{
<tr>
<td>@user.Id</td>
<td>@user.Username</td>
<td>@user.Email</td>
<td>@user.FirstName</td>
<td>@user.LastName</td>
</tr>
}
</tbody>
</table>
}
</div>
</div>
IndexViewModel.cs
public class IndexViewModel : ComponentBase, IDisposable
{
#region Private Members
private readonly CancellationTokenSource cts = new CancellationTokenSource();
private bool disposedValue = false; // To detect redundant calls

[Inject]
private IToastService ToastService { get; set; }
#endregion

#region Protected Members
protected List<User> users;

[Inject] IUsersService UsersService { get; set; }

protected string ErrorMessage { get; set; }

#endregion

#region Constructor

public IndexViewModel()
{
users = new List<User>();
}

#endregion

#region Public Methods


#endregion

#region Private Methods

protected override async Task OnInitializedAsync()
{
await GetUsers().ConfigureAwait(false);
}

private async Task GetUsers()
{
try
{
await foreach (var user in UsersService.GetAllUsers(cts.Token))
{
users.Add(user);
StateHasChanged();
}
}
catch (OperationCanceledException)
{
ShowErrorMessage($"{ nameof(GetUsers) } was canceled at user's request.", "Canceled");
}

catch (Exception ex)
{
// TODO: Log the exception and filter the exception messages which are displayed to users.
ShowErrorMessage(ex.Message);
}
}

private void ShowErrorMessage(string message, string heading ="")
{
//ErrorMessage = message;
//StateHasChanged();
ToastService.ShowError(message, heading);
}

private void ShowSuccessMessage(string message, string heading = "")
{
ToastService.ShowSuccess(message, heading);
}

protected void Cancel()
{
cts.Cancel();
}
#endregion

#region IDisposable Support

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
cts.Dispose();
}

disposedValue = true;
}
}

public void Dispose()
{
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);
}
#endregion
}

关于code-behind - Blazor 服务器 - 'Code Behind' 模式 : OnInitializedAsync(): no suitable method found to override,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59051233/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com