共用方式為


ConnectivityStatus.IsOffline 屬性

定義

取得或設定值,這個值表示應用程式是否處於離線模式。

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 驗證、角色和設定檔服務。

適用於

另請參閱