diff --git a/Microsoft.Toolkit.Uwp.UI/Triggers/NetworkConnectionStateTrigger.cs b/Microsoft.Toolkit.Uwp.UI/Triggers/NetworkConnectionStateTrigger.cs index 7f58966bc55..20ec63445c9 100644 --- a/Microsoft.Toolkit.Uwp.UI/Triggers/NetworkConnectionStateTrigger.cs +++ b/Microsoft.Toolkit.Uwp.UI/Triggers/NetworkConnectionStateTrigger.cs @@ -3,6 +3,7 @@ // 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; @@ -14,6 +15,8 @@ namespace Microsoft.Toolkit.Uwp.UI.Triggers /// public class NetworkConnectionStateTrigger : StateTriggerBase { + private readonly WeakEventListener _weakEvent; + private DispatcherQueue _dispatcherQueue; /// @@ -22,13 +25,14 @@ public class NetworkConnectionStateTrigger : StateTriggerBase public NetworkConnectionStateTrigger() { _dispatcherQueue = DispatcherQueue.GetForCurrentThread(); - var weakEvent = - new WeakEventListener(this) - { - OnEventAction = (instance, source) => NetworkInformation_NetworkStatusChanged(source), - OnDetachAction = (weakEventListener) => NetworkInformation.NetworkStatusChanged -= weakEventListener.OnEvent - }; - NetworkInformation.NetworkStatusChanged += weakEvent.OnEvent; + + _weakEvent = new WeakEventListener(this) + { + OnEventAction = static (instance, source, eventArgs) => { instance.NetworkInformation_NetworkStatusChanged(source); }, + OnDetachAction = listener => { NetworkInformation.NetworkStatusChanged -= OnNetworkEvent; } + }; + + NetworkInformation.NetworkStatusChanged += OnNetworkEvent; UpdateState(); } @@ -37,6 +41,11 @@ 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; @@ -69,70 +78,6 @@ private static void OnConnectionStatePropertyChanged(DependencyObject d, Depende var obj = (NetworkConnectionStateTrigger)d; obj.UpdateState(); } - - private class WeakEventListener - where TInstance : class - { - /// - /// WeakReference to the instance listening for the event. - /// - private WeakReference _weakInstance; - - /// - /// Gets or sets the method to call when the event fires. - /// - public Action OnEventAction { get; set; } - - /// - /// Gets or sets the method to call when detaching from the event. - /// - public Action> OnDetachAction { get; set; } - - /// - /// Initializes a new instance of the class. - /// - /// Instance subscribing to the event. - public WeakEventListener(TInstance instance) - { - if (instance == null) - { - throw new ArgumentNullException("instance"); - } - - _weakInstance = new WeakReference(instance); - } - - /// - /// Handler for the subscribed event calls OnEventAction to handle it. - /// - /// Event source. - public void OnEvent(TSource source) - { - TInstance target = (TInstance)_weakInstance.Target; - if (target != null) - { - // Call registered action - OnEventAction?.Invoke(target, source); - } - else - { - // Detach from event - Detach(); - } - } - - /// - /// Detaches from the subscribed event. - /// - public void Detach() - { - if (OnDetachAction != null) - { - OnDetachAction(this); - OnDetachAction = null; - } - } - } } ///