ConnectivityStatus 類別

定義

代表應用程式網路連線的狀態。

public ref class ConnectivityStatus abstract sealed
public static class ConnectivityStatus
type ConnectivityStatus = class
Public Class ConnectivityStatus
繼承
ConnectivityStatus

範例

下列範例程式碼示範如何使用這個類別,根據核取方塊值來更新離線狀態。 在此範例中 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

備註

使用 類別 ConnectivityStatus 來指定使用用戶端應用程式服務的 Windows 應用程式網路連線能力。 當您將 IsOffline 屬性設定為 true 時,用戶端服務提供者會使用離線快取,而不是嘗試存取對應的 Web 服務。 離線狀態會以每個使用者和個別應用程式為基礎儲存。 如需詳細資訊,請參閱如何:使用用戶端應用程式服務離線工作

屬性

IsOffline

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

適用於

另請參閱