This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathNetworkConnectionStateTrigger.cs
More file actions
98 lines (83 loc) · 3.45 KB
/
NetworkConnectionStateTrigger.cs
File metadata and controls
98 lines (83 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Microsoft.Toolkit.Uwp.Helpers;
using Windows.Networking.Connectivity;
using Windows.System;
using Windows.UI.Xaml;
namespace Microsoft.Toolkit.Uwp.UI.Triggers
{
/// <summary>
/// Trigger for switching when the network availability changes
/// </summary>
public class NetworkConnectionStateTrigger : StateTriggerBase
{
private readonly WeakEventListener<NetworkConnectionStateTrigger, object, EventArgs> _weakEvent;
private DispatcherQueue _dispatcherQueue;
/// <summary>
/// Initializes a new instance of the <see cref="NetworkConnectionStateTrigger"/> class.
/// </summary>
public NetworkConnectionStateTrigger()
{
_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
_weakEvent = new WeakEventListener<NetworkConnectionStateTrigger, object, EventArgs>(this)
{
OnEventAction = (instance, source, eventArgs) => { NetworkInformation_NetworkStatusChanged(source); },
OnDetachAction = listener => { NetworkInformation.NetworkStatusChanged -= OnNetworkEvent; }
};
NetworkInformation.NetworkStatusChanged += OnNetworkEvent;
UpdateState();
}
private void NetworkInformation_NetworkStatusChanged(object sender)
{
_ = _dispatcherQueue.EnqueueAsync(UpdateState, DispatcherQueuePriority.Normal);
}
private void OnNetworkEvent(object source)
{
_weakEvent?.OnEvent(source, EventArgs.Empty);
}
private void UpdateState()
{
bool isConnected = false;
var profile = NetworkInformation.GetInternetConnectionProfile();
if (profile != null)
{
isConnected = profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
}
SetActive((isConnected && ConnectionState == ConnectionState.Connected) || (!isConnected && ConnectionState == ConnectionState.Disconnected));
}
/// <summary>
/// Gets or sets the state of the connection to trigger on.
/// </summary>
public ConnectionState ConnectionState
{
get { return (ConnectionState)GetValue(ConnectionStateProperty); }
set { SetValue(ConnectionStateProperty, value); }
}
/// <summary>
/// Identifies the <see cref="ConnectionState"/> DependencyProperty
/// </summary>
public static readonly DependencyProperty ConnectionStateProperty =
DependencyProperty.Register(nameof(ConnectionState), typeof(ConnectionState), typeof(NetworkConnectionStateTrigger), new PropertyMetadata(ConnectionState.Connected, OnConnectionStatePropertyChanged));
private static void OnConnectionStatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var obj = (NetworkConnectionStateTrigger)d;
obj.UpdateState();
}
}
/// <summary>
/// ConnectionStates
/// </summary>
public enum ConnectionState
{
/// <summary>
/// Connected
/// </summary>
Connected,
/// <summary>
/// Disconnected
/// </summary>
Disconnected,
}
}