ConnectivityStatus.IsOffline 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定值,這個值表示應用程式是否處於離線模式。
public:
static property bool IsOffline { bool get(); void set(bool value); };
public static bool IsOffline { get; set; }
static member IsOffline : bool with get, set
Public Shared Property IsOffline As Boolean
屬性值
如果應用程式處於離線模式,則為 true
,否則為 false
。
範例
下列範例程式碼示範如何使用這個屬性,根據核取方塊值來更新離線狀態。 在此範例中 CheckedChanged ,事件處理常式會更新離線狀態。 如果使用者將應用程式設定為線上狀態,事件處理常式會嘗試重新驗證使用者。 不過,如果驗證服務器無法使用,事件處理常式會將應用程式傳回離線狀態。
private void workOfflineCheckBox_CheckedChanged(
object sender, EventArgs e)
{
ConnectivityStatus.IsOffline = workOfflineCheckBox.Checked;
if (!ConnectivityStatus.IsOffline)
{
try
{
// Silently re-validate the user.
((ClientFormsIdentity)
System.Threading.Thread.CurrentPrincipal.Identity)
.RevalidateUser();
// If any settings have been changed locally, save the new
// new values to the Web settings service.
SaveSettings();
// If any settings have not been changed locally, check
// the Web settings service for updates.
Properties.Settings.Default.Reload();
}
catch (System.Net.WebException)
{
MessageBox.Show(
"Unable to access the authentication service. " +
Environment.NewLine + "Staying in offline mode.",
"Warning", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
workOfflineCheckBox.Checked = true;
}
}
}
Private Sub workOfflineCheckBox_CheckedChanged( _
ByVal sender As Object, ByVal e As EventArgs) _
Handles workOfflineCheckBox.CheckedChanged
ConnectivityStatus.IsOffline = workOfflineCheckBox.Checked
If Not ConnectivityStatus.IsOffline Then
Try
' Silently re-validate the user.
CType(System.Threading.Thread.CurrentPrincipal.Identity, _
ClientFormsIdentity).RevalidateUser()
' If any settings have been changed locally, save the new
' new values to the Web settings service.
SaveSettings()
' If any settings have not been changed locally, check
' the Web settings service for updates.
My.Settings.Reload()
Catch ex As System.Net.WebException
MessageBox.Show( _
"Unable to access the authentication service. " & _
Environment.NewLine + "Staying in offline mode.", _
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
workOfflineCheckBox.Checked = True
End Try
End If
End Sub
備註
當您設定 IsOffline 屬性時, ConnectivityStatus 類別會將值快取至本機檔案系統。 命名空間中的 System.Web.ClientServices.Providers 用戶端服務提供者會檢查此值,以判斷是否要使用離線資料快取,而不是嘗試存取相關聯的 Microsoft Ajax 驗證、角色和設定檔服務。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應