Blazor异常消息显示

Blazor异常消息显示1.打开/Share/MainLayout.razor文件用ErrorBoundary标签包果@Body内容。<ErrorBoundary@ref=”_errorBoundary”><ReuseTabsClass=”top-rowpx-4″TabPaneClass=”contentpx-4″/><BlazoredToastsPosition=”To

大家好,欢迎来到IT知识分享网。Blazor异常消息显示"

1. 打开/Share/MainLayout.razor文件

用 ErrorBoundary标签包果@Body内容。

 <ErrorBoundary @ref="_errorBoundary">
                        <ReuseTabs Class="top-row px-4" TabPaneClass="content px-4" />

                        <BlazoredToasts Position="ToastPosition.BottomRight"
                                        Timeout="3"
                                        IconType="Blazored.Toast.IconType.FontAwesome"
                                        SuccessClass="success-toast-override"
                                        SuccessIcon="fa fa-thumbs-up"
                                        ErrorIcon="fa fa-bug" />
</ErrorBoundary>
@code {
    private ErrorBoundary? _errorBoundary;

    protected override void OnParametersSet()
    {//显示错误后恢复UI响应
        _errorBoundary?.Recover();
    }
}

2. 打开Share/App.razor

用<Error>标签包果路由

 <Error>
        <Router AppAssembly="@typeof(App).Assembly">
            <Found Context="routeData">
                @*<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />*@
                <ReuseTabsRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
                <FocusOnNavigate RouteData="@routeData" Selector="h1" />
            </Found>
            <NotFound>
                <PageTitle>Not found</PageTitle>
                <LayoutView Layout="@typeof(MainLayout)">
                    <p role="alert">Sorry, there's nothing at this address.</p>
                </LayoutView>
            </NotFound>
        </Router>
    </Error>

3. 创建 Share/Error.razor 错误信息显示组件

@using Microsoft.Extensions.Logging
@inject ILogger<Error> Logger
@inject MessageService _message

<div>
    @if (isErrorActive)
    {
        <div class="alert alert-danger" role="alert">
            <Button Type="button" Class="btn-close " data-dismiss="alert" aria-label="Close" @onclick="HideError">
                <span aria-hidden="true">&times;</span>
            </Button>
            <h3>@title</h3>
            <p>@message</p>
        </div>
    }
    <CascadingValue Value="this">
        @ChildContent
    </CascadingValue>
</div>

@code {
    bool isErrorActive = false;
    string title;
    string message;
    [Parameter]
    public RenderFragment? ChildContent { get; set; }
    public async void ProcessError(Exception ex)
    {
        string message = $"错误信息:{ex.Message}";
        Logger.LogError(message);

        this.isErrorActive = false;
        this.title = "Error:ProcesError";
        this.message = message;
        await ErrorMessage(this.message);
    }
    private void HideError()
    {
        isErrorActive = false;
    }
     private async Task ErrorMessage(string message)
    {
        await _message.Error(message);

    }
    }

4. 使用Error组件

@code {
  
    [CascadingParameter]
    public Error? error{ get; set; }

    private void IncrementCount()
    {
        currentCount++;
        try
        {
            if (currentCount >= 5)
            {
                throw new InvalidOperationException("无效的操作");
            }
        }
        catch(Exception ex)
        {
            error?.ProcessError(ex);
        }
    }
}

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/15176.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信