You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shell应用中BlazorWebView首次正常,返回重进时卡顿无响应

BlazorWebViewPage 二次访问冻结问题

我有一个用于演示的Shell应用,其中包含BlazorWebViewPage页面,该页面首次可成功打开Blazor网站,但仅能正常工作一次。再次访问时,页面无法打开且导致应用冻结。退出Blazor页面可正常返回MainViewsPage,其他页面导航正常,但再次点击进入BlazorWebView时,应用冻结无响应。

开发参考内容:
-.NET MAUI Blazor Hybrid App代码

相关代码片段

Shell配置

<ShellContent
    Title="Views"
    ContentTemplate="{DataTemplate viewPages:MainViewsPage}"
    Route="MainViewsPage" />

DI注册(期望保留页面状态)

mauiAppBuilder.Services.AddSingleton<BlazorWebViewPage>();

路由添加代码

AddPage<BlazorWebViewPage>();

private void AddPage<TPage>() where TPage : ContentPage
{
    Routes.Add(typeof(TPage).Name, typeof(TPage));
    Routing.RegisterRoute(typeof(TPage).Name, typeof(TPage));
}

BlazorWebViewPage页面定义

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
        x:Class="Learning.Pages.UserInterface.Controls.Views.BlazorWebViewPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:Learning"
    Title="Blazor Web View">
    
    <BlazorWebView x:Name="blazorWebView" HostPage="wwwroot/index.html">
        <BlazorWebView.RootComponents>
            <RootComponent ComponentType="{x:Type local:Components.Routes}" Selector="#app" />
        </BlazorWebView.RootComponents>
    </BlazorWebView>
    
</ContentPage>

页面后台代码

public partial class BlazorWebViewPage : ContentPage
{
    public BlazorWebViewPage()
    {
        InitializeComponent();
    }
}

Blazor退出菜单

<div class="nav-item px-3">
    <a class="nav-link" @onclick="NavigateToShellRoute">
        <span class="bi bi-exit-nav-menu" aria-hidden="true"></span> 退出Blazor
    </a>
</div>

退出导航代码

@code {
    private async Task NavigateToShellRoute()
    {
        await Shell.Current.GoToAsync("..");
    }
}

MainViewsPage导航按钮

<Button
    Command="{Binding GoToRouteCommand}"
    CommandParameter="BlazorWebViewPage"
    Text="Blazor WebView" />

导航命令代码

public ICommand GoToRouteCommand { get; private set; }

GoToRouteCommand = new Command<string>(async (route) => await GoToAsync(route));

内容的提问来源于stack exchange,提问作者Derek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 05:23:13