-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Removed private WeakEventListener #4450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | |
/// </summary> | ||
public class NetworkConnectionStateTrigger : StateTriggerBase | ||
{ | ||
private readonly WeakEventListener<NetworkConnectionStateTrigger, object, EventArgs> _weakEvent; | ||
|
||
private DispatcherQueue _dispatcherQueue; | ||
|
||
/// <summary> | ||
|
@@ -22,13 +25,14 @@ public class NetworkConnectionStateTrigger : StateTriggerBase | |
public NetworkConnectionStateTrigger() | ||
{ | ||
_dispatcherQueue = DispatcherQueue.GetForCurrentThread(); | ||
var weakEvent = | ||
new WeakEventListener<NetworkConnectionStateTrigger, object>(this) | ||
{ | ||
OnEventAction = (instance, source) => NetworkInformation_NetworkStatusChanged(source), | ||
OnDetachAction = (weakEventListener) => NetworkInformation.NetworkStatusChanged -= weakEventListener.OnEvent | ||
}; | ||
NetworkInformation.NetworkStatusChanged += weakEvent.OnEvent; | ||
|
||
_weakEvent = new WeakEventListener<NetworkConnectionStateTrigger, object, EventArgs>(this) | ||
{ | ||
OnEventAction = (instance, source, eventArgs) => { NetworkInformation_NetworkStatusChanged(source); }, | ||
OnDetachAction = listener => { NetworkInformation.NetworkStatusChanged -= OnNetworkEvent; } | ||
}; | ||
|
||
NetworkInformation.NetworkStatusChanged += OnNetworkEvent; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should just be I wonder if we need to look at the history, the original developer probably had the copy because of the strange delegate from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We can probably use the sender to access fields on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @puppetsw Let's do the same thing for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Arlodotexe I'm sorry, but I'm not sure how to go about this, or I'm not understanding. If you could give a pointer I'm happy to make the change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dug into this a bit. Looks like @michael-hawker does that sound right? Should be able to move forward if so. |
||
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<TInstance, TSource> | ||
where TInstance : class | ||
{ | ||
/// <summary> | ||
/// WeakReference to the instance listening for the event. | ||
/// </summary> | ||
private WeakReference _weakInstance; | ||
|
||
/// <summary> | ||
/// Gets or sets the method to call when the event fires. | ||
/// </summary> | ||
public Action<TInstance, TSource> OnEventAction { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the method to call when detaching from the event. | ||
/// </summary> | ||
public Action<WeakEventListener<TInstance, TSource>> OnDetachAction { get; set; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="WeakEventListener{TInstance, TSource}"/> class. | ||
/// </summary> | ||
/// <param name="instance">Instance subscribing to the event.</param> | ||
public WeakEventListener(TInstance instance) | ||
{ | ||
if (instance == null) | ||
{ | ||
throw new ArgumentNullException("instance"); | ||
} | ||
|
||
_weakInstance = new WeakReference(instance); | ||
} | ||
|
||
/// <summary> | ||
/// Handler for the subscribed event calls OnEventAction to handle it. | ||
/// </summary> | ||
/// <param name="source">Event source.</param> | ||
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(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Detaches from the subscribed event. | ||
/// </summary> | ||
public void Detach() | ||
{ | ||
if (OnDetachAction != null) | ||
{ | ||
OnDetachAction(this); | ||
OnDetachAction = null; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
|
Uh oh!
There was an error while loading. Please reload this page.