简体   繁体   English

System.InvalidOperationException:“尚未分配渲染句柄。” 在 Blazor 服务器中

[英]System.InvalidOperationException: 'The render handle is not yet assigned.' in blazor server

I have created a simple blazor application and I need to show a loading screen on the index.razor page ,I am not needed to include it on the index.razor page.i am using some indirect way to do it .我创建了一个简单的 blazor 应用程序,我需要在 index.razor 页面上显示一个加载屏幕,我不需要将它包含在 index.razor 页面上。我正在使用一些间接的方式来做到这一点。

first I have created a one class called Appsettings.cs and brought the loading logic inside it首先,我创建了一个名为 Appsettings.cs 的类并将加载逻辑带入其中

Appsettings.cs应用设置.cs

public class AppSettings
    {
        public static bool _IsProcessing { get; set; } = false;

        public static MainLayout _layout { get; set; } = new MainLayout();

        public static void Loading(bool isProcessing)
        {
            _IsProcessing = isProcessing;

            if(_layout !=null)
               _layout .LoadingScreenShowing(_IsProcessing);
        }

    }

then my index.razor file is like this ,when press the below Load button ,I need to show the Loading screen .然后我的 index.razor 文件是这样的,当按下下面的加载按钮时,我需要显示加载屏幕。

index.razor index.razor

<button onclick="PageLoading">Load</button>
@code{
  protected override void Oninitialized(){}

  private void PageLoading(){
     AppSettings.Loading(true);
     this.StateHasChanged();
     //do something
     AppSettings.Loading(false);
     this.StateHasChanged();
         
  }

after I have included loading part into the MainLayout.razor not explicitly to the index.razor在我将部分加载到 MainLayout.razor 中后,没有明确地添加到 index.razor

MainLayout.razor MainLayout.razor

@inherits LayoutComponentBase

<PageTitle>BlazorApp1</PageTitle>

<div class="page">
<div class="sidebar">
    <NavMenu />
</div>

    <main>
        <div class="top-row px-4">
            <a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
        </div>

        <article class="content px-4">
            <Loading IsVisible="@IsLoading" @ref="load"/>  //loading component 
            @Body
        </article>
    </main>
</div>

And I've created a partial class to place the functional part of the main layout MainLayout.razor.cs我创建了一个局部类来放置主布局 MainLayout.razor.cs 的功能部分

public partial class MainLayout{
        private bool IsLoading;
        Loading load ;

        public async void LoadingScreenShowing(bool isLoading)
        {
            load = new Loading();
            IsLoading = isLoading;
            this.StateHasChanged();//exception is throwing from this line 
            await Task.CompletedTask;
        }
}

when execute the this.StateHasChanged() line ,I am getting the exception called当执行 this.StateHasChanged() 行时,我得到了异常调用

System.InvalidOperationException: 'The render handle is not yet assigned.' System.InvalidOperationException:“尚未分配渲染句柄。”

but I don't know why It's occurring ,pls help me但我不知道为什么会发生,请帮助我

You can't do it the way your are trying to.你不能按照你想要的方式去做。 Component lifecycles are managed by the Renderer: you can't create a component instance and somehow shoehorn it into a page.组件生命周期由渲染器管理:您不能创建组件实例并以某种方式将其硬塞到页面中。 That's the reason for the error.这就是错误的原因。

Here's a heavily refactored version of your code that I think does what intend.这是您的代码的一个经过大量重构的版本,我认为它符合预期。

The scoped service that maintains the App State (your AppSettings ):维护应用程序状态的范围服务(您的AppSettings ):

public class AppStateService
{
    private bool _isLoaded;

    public event Action? LoadStateChanged;

    public bool IsLoaded
    {
        get => _isLoaded;
        set
        {
            if (_isLoaded != value)
            {
                _isLoaded = value;
                LoadStateChanged?.Invoke();
            }
        }
    }
}

registered in program:在程序中注册:

builder.Services.AddScoped<AppStateService>();

The Loading component - I've used a simple bootstrap alert to display the loading message.加载组件 - 我使用了一个简单的引导警报来显示加载消息。

@inject AppStateService appStateService
@implements IDisposable

@if (!this.appStateService.IsLoaded)
{
    <div class="alert alert-danger">
        Page Loading...
    </div>
}

@code {
    protected override void OnInitialized()
        => this.appStateService.LoadStateChanged += this.OnStateChanged;

    private void OnStateChanged()
        => this.InvokeAsync(StateHasChanged);

    public void Dispose()
        => this.appStateService.LoadStateChanged -= this.OnStateChanged;
}

MainLayout:主布局:

@inherits LayoutComponentBase

<PageTitle>BlazorApp3</PageTitle>

<div class="page">
    <div class="sidebar">
        <NavMenu />
    </div>

    <main>
        <div class="top-row px-4 auth">
            <LoginDisplay />
            <a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
        </div>

        <article class="content px-4">
            <Loading />
            @Body
        </article>
    </main>
</div>

And finally the demo page:最后是演示页面:

@page "/"
@inject AppStateService appStateService; 

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

Welcome to your new app.

<SurveyPrompt Title="How is Blazor working for you?" />

@code{

    protected async override Task OnInitializedAsync()
    {
        this.appStateService.IsLoaded = false;
        // emulate an async loading event 
        await Task.Delay(2000);
        this.appStateService.IsLoaded = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Blazor 服务器 - System.InvalidOperationException 随机抛出 - Blazor Server - System.InvalidOperationException is throwing randomly 如何在实体框架中处理System.InvalidOperationException? - How to handle System.InvalidOperationException in entity framework? Blazor:System.InvalidOperationException:“RemoteNavigationManager”尚未初始化 - Blazor : System.InvalidOperationException: 'RemoteNavigationManager' has not been initialized Blazor 服务器端应用程序在重新加载期间抛出“System.InvalidOperationException:此时无法发出 JavaScript 互操作调用。...” - Blazor server side application throwing "System.InvalidOperationException: JavaScript interop calls cannot be issued at this time. ..." during reload Nunit System.InvalidOperationException - Nunit System.InvalidOperationException System.InvalidOperationException Android - System.InvalidOperationException Android HttpResponseMessage System.InvalidOperationException - HttpResponseMessage System.InvalidOperationException 带有SQlBulkCopy的System.InvalidOperationException - System.InvalidOperationException with SQlBulkCopy 系统无效操作异常 - system.invalidoperationexception XmlSerializer System.InvalidOperationException - XmlSerializer System.InvalidOperationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM