Asp.net-Core

如何更改 Blazor 中的“無法重新連接到伺服器”文本?

  • July 24, 2020

我正在使用 Blazor 伺服器端。

當 Blazor 應用程序斷開與遠端伺服器的連接時,它將顯示以下內容:

在此處輸入圖像描述

我想更改上圖中的文本(“無法重新連接到伺服器……”等等)。

我想把它改成我們國家的語言。

我找到了該項目的文件,但對此一無所獲。

我怎樣才能改變它?謝謝你。

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>

然後讓我們添加一些 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>

最後,在嘗試連接或連接失敗時,我們將收到以下消息:

在此處輸入圖像描述

引用自:https://stackoverflow.com/questions/58404533