如何更改 Blazor 中的“无法重新连接到服务器”文本?

Mel*_* NG 13 .net-core asp.net-core blazor

我正在使用 Blazor 服务器端。

当 Blazor 应用程序断开与远程服务器的连接时,它将显示:

在此处输入图片说明

我想更改上图的文本(“无法重新连接到服务器...”等)。

我想把它改成我们国家的语言。

我找到了该项目的文件,但一无所获。

我怎样才能改变它?谢谢你。

itm*_*nus 32

Blazor 应用程序将检查页面中是否有id= 的 html 元素{dialogId}

  1. 如果这样的元素不存在,它将使用默认处理程序来显示消息。
  2. 如果此元素存在,则此元素class将是:
    • 设置为components-reconnect-show尝试重新连接到服务器时。
    • 设置为components-reconnect-failed重新连接失败时,可能是由于网络故障。要尝试重新连接,请调用window.Blazor.reconnect()
    • 设置为components-reconnect-rejected重新连接被拒绝时。到达服务器但拒绝连接,用户在服务器上的状态丢失。要重新加载应用程序,请调用location.reload()

默认情况下,dialogIdcomponents-reconnect-modal。所以你可以在页面中创建一个元素,并使用 CSS 来控制你喜欢的内容和样式。

查看Microsoft Docs了解最新信息。

演示:

例如,我创建了三部分内容以显示在Pages/_Host.cshtml

<div id="components-reconnect-modal" class="my-reconnect-modal components-reconnect-hide">
    <div class="show">
        <p>
            This is the message when attempting to connect to server
        </p>
    </div>
    <div class="failed">
        <p>
            This is the custom message when failing 
        </p>
    </div>
    <div class="rejected">
        <p>
            This is the custom message when refused
        </p>
    </div>
</div>

<app>
    @(await Html.RenderComponentAsync<App>(RenderMode.ServerPrerendered))
</app>

<script src="_framework/blazor.server.js"></script>
Run Code Online (Sandbox Code Playgroud)

然后让我们添加一些 CSS 来控制样式:

<style>
    .my-reconnect-modal > div{
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        z-index: 1000;
        overflow: hidden;
        background-color: #fff;
        opacity: 0.8;
        text-align: center;
        font-weight: bold;
    }
    .components-reconnect-hide > div
    {
        display: none;
    }

    .components-reconnect-show > div
    {
        display: none;
    }
    .components-reconnect-show > .show
    {
        display: block;
    }

    .components-reconnect-failed > div
    {
        display: none;
    }
    .components-reconnect-failed > .failed
    {
        display: block;
    }

    .components-reconnect-rejected >div
    {
        display: none;
    }
    .components-reconnect-rejected > .rejected
    {
        display: block;
    }
</style>
Run Code Online (Sandbox Code Playgroud)

最后,当尝试连接或连接失败时,我们将收到以下消息:

在此处输入图片说明

  • @AaronHudon 请参阅[此处的官方文档](https://docs.microsoft.com/en-us/aspnet/core/blazor/hosting-models?view=aspnetcore-3.1#reflect-the-connection-state-in-the -ui) (2认同)
  • 此信息似乎已移至[此处](https://docs.microsoft.com/en-us/aspnet/core/blazor/hosting-model-configuration?view=aspnetcore-3.1#reflect-the-connection-state -在用户界面中)。 (2认同)

Pos*_*rte 15

对于 javascript 方面,Blazor 通过window.Blazor对象公开了一个很小的 ​​API 。

此 API 的一部分是defaultReconnectionHandler允许您自定义重新连接体验,包括为重试次数等设置不同的选项。

但是,也可以只换出用于显示的逻辑 ReconnectionDisplay

一个简单的实现如下所示,使您能够控制流程:

function createOwnDisplay() {
    return {
        show: () => { alert("put code that shows a toast , ui, or whaterver here"); },
        hide: () => { console.log('foo'); },
        failed: () => { console.log('foo'); },
        rejected: () => { console.log('foo'); }
    };
}

Blazor.defaultReconnectionHandler._reconnectionDisplay = createOwnDisplay();
Run Code Online (Sandbox Code Playgroud)