Blazor Server:导航到其他页面后,NavigationManager停止页面

发布于 2025-02-04 01:22:32 字数 601 浏览 2 评论 0原文

带有“ Page1”,“ Page2”,“ Page3”,“ Pagex”菜单项的自定义菜单组件的julazor。 单击“ Page1”时,我使用NavigationManager导航到其他页面。

这一切都很好。

但是,当我快速单击“ page1”,“ page2”或2个或更多页的其他组合时,我有一个问题,即我的页面加载了,但直接加载了另一页,因为我迅速单击了菜单项。 我的页面包含异步/等待方法,在这里我遇到了一个问题:

  1. 快速单击“ page3”,单击“ page1”,单击“ page2”
  2. 导航到“ page3”
  3. ynnc方法中的“ page3” ynk in in in of page3启动(使用async/agait/events附加/...)
  4. 突然导航发生到“ Page1”
  5. 大麻应用程序崩溃,因为例如,异步任务返回数据或从“ Page3”访问组件,该组件已经被处置了,因为我们导航到“ Page1”,
  6. 如果我们很幸运,到达“ Page1”,但可能会发生同样的原因,因为当导航发生时直接发生“ Page2”的导航

,是否有一种方法可以在导航到其他页面时“取消”当前页面? (例如,也许类似于取消词?)

Blazor with a custom menu component having "Page1", "Page2", "Page3, "PageX" menu items.
When clicking "Page1" I navigate to a different page using NavigationManager.

This works all fine.

However when I quickly click "Page1","Page2", or any other combination of 2 or more pages I have a problem that my Page1 loads but directly another Page is loaded because I quickly clicked menu items.
My pages contain async/await methods and here I have the problem for example:

  1. quickly click "Page3", click "Page1", click "Page2"
  2. Navigation occurs to "Page3"
  3. async methods in Page3 kick in (using async/await/events attached/...)
  4. Suddenly navigation occurs to "Page1"
  5. Blazor app crashed because for example an async task returned data or accesses a component from "Page3", which has been disposed already because we navigate to "Page1"
  6. If we are lucky we reached "Page1", but same might happen because a navigation to "Page2" occurs directly

When a navigation occurs, is there a way to "cancel" the current page when navigating to a different page? (E.g. maybe something similar to a CancellationToken ?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花想c 2025-02-11 01:22:32
  • “ page3”怎么样?
  • 您在页面上的异步是什么?
  • 您是否在页面/组件中进行直接DB访问?

Blazor应用不应该崩溃。所有页面都是由Renderer管理其生命周期的组件。在完成渲染之前,可以将它们从渲染中删除,但是渲染器仍将等待其事件周期的完成,然后再提及它们:他们将完成活动周期。

这是一个演示计数器,供您快速单击Thro并查看会发生什么。当从渲染树中删除组件时,处置被调用,但是
生命周期仍然完成。

@page "/counter"
@using System.Diagnostics

@implements IDisposable

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

<div>
    Message: @message
</div>
@code {
    private int currentCount = 0;

    private string message = string.Empty;

    private void IncrementCount()
    {
        currentCount++;
    }

    protected async override Task OnInitializedAsync()
    {
       Debug.WriteLine($"Started Load at {DateTime.Now.ToLongTimeString()}");
        message = "Loading";
        await Task.Delay(10000);
        message = "Loaded";
       Debug.WriteLine($"Finished Load at {DateTime.Now.ToLongTimeString()}");
    }

    public void Dispose()
    {
         Debug.WriteLine($"Disposed at {DateTime.Now.ToLongTimeString()}");
    }
}
  • What does say "Page3" looks like?
  • What are you doing async in the page?
  • Are you doing direct DB access in your page/component?

The Blazor App shouldn't crash. All the pages are components whose lifecycle is managed by the Renderer. They may be removed from the RenderTree before they have completed rendering, but the Renderer will still await the completion of their event cycle before dropping it's reference to them: they will complete their event cycle.

Here's a demo counter for you to quickly click thro and see what happens. Dispose gets called when the component in removed from the RenderTree, but the
lifecycle still completes.

@page "/counter"
@using System.Diagnostics

@implements IDisposable

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

<div>
    Message: @message
</div>
@code {
    private int currentCount = 0;

    private string message = string.Empty;

    private void IncrementCount()
    {
        currentCount++;
    }

    protected async override Task OnInitializedAsync()
    {
       Debug.WriteLine(
quot;Started Load at {DateTime.Now.ToLongTimeString()}");
        message = "Loading";
        await Task.Delay(10000);
        message = "Loaded";
       Debug.WriteLine(
quot;Finished Load at {DateTime.Now.ToLongTimeString()}");
    }

    public void Dispose()
    {
         Debug.WriteLine(
quot;Disposed at {DateTime.Now.ToLongTimeString()}");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文