【问题标题】:How can I change the "Could not reconnect to the server" text in Blazor?如何更改 Blazor 中的“无法重新连接到服务器”文本?
【发布时间】:2020-02-12 17:12:06
【问题描述】:

我正在使用 Blazor 服务器端。

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

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

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

我找到了该项目的文件,但没有找到任何相关内容。

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

【问题讨论】:

    标签: asp.net-core .net-core blazor


    【解决方案1】:

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

    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>
    

    然后让我们添加一些 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>
    

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

    【讨论】:

    • 这是很好的信息,但是 Microsoft Docs 中的任何文档在哪里?
    • @AaronHudon 见official docs here
    • 谢谢。奇怪的是他们把它放在托管模型
    • 您的风格似乎有一个小错误。名为components-reconnect-refused 的类应正确命名为components-reconnect-rejected
    • 此信息似乎已移至here
    【解决方案2】:

    对于 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();
    

    【讨论】:

    • 嗯,这也是一种解决方法。但我更喜欢@itminus 的方式。还是谢谢大家。
    • 当然,这取决于您的用例。如果您需要更多控制(例如,在连接失败时执行自定义代码),则可以使用 API。如果你只是想换掉 UI,你可以使用 @itminus 的建议。
    【解决方案3】:

    在你的 CSS 中:

    #components-reconnect-modal {
        display: none;
        position: fixed;
        top: 0px;
        right: 0px;
        bottom: 0px;
        left: 0px;
        z-index: 1000;
        overflow: hidden;
        background-color: rgb(255, 255, 255);
        opacity: 0.8;
        text-align: center;
        font-weight: bold;
    }
    #components-reconnect-modal.components-reconnect-show{
        display: block;
    }
    #components-reconnect-modal.components-reconnect-show div.reconnecting {
        display: block;
    }
    div.reconnecting {
        display: none;
    }
    
    #components-reconnect-modal.components-reconnect-failed {
        display: block;
    }
    #components-reconnect-modal.components-reconnect-failed div.failedToConnect {
        display: block;
    }
    div.failedToConnect {
        display: none;
    }
    
    #components-reconnect-modal.components-reconnect-rejected {
        display: block;
    }
    #components-reconnect-modal.components-reconnect-rejected div.connectionRejected {
        display: block;
    }
    div.connectionRejected {
        display: none;
    }
    
    #components-reconnect-modal.components-reconnect-hide {
        display: none;
    }
    

    在_Host正文中:

    <div id="components-reconnect-modal">
        <div class="reconnecting">
            Подключение к серверу...
        </div>
        <div class="failedToConnect">
            Не удалось подключиться к серверу <input type="button" value="переподключиться" onclick="ReconnectToServer()" />
            <script>
                function ReconnectToServer() {
                    window.Blazor.reconnect();
                }
            </script>
        </div>
        <div class="connectionRejected">
            Подключение к серверу прервано <input type="button" value="обновить страницу" onclick="ReloadPage()" />
            <script>
                function ReloadPage() {
                    location.reload();
                }
            </script>
        </div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-25
      • 2022-07-18
      • 2020-05-03
      • 2015-03-01
      • 1970-01-01
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多