Skip to content

Commit 27fcb01

Browse files
committed
Added loaded event usage to Marquee to reduce events while unloaded and not disposed
1 parent 29bcd00 commit 27fcb01

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

components/Marquee/src/Marquee.Events.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,29 @@ public partial class Marquee
2323
/// Event raised when the Marquee completes scrolling.
2424
/// </summary>
2525
public event EventHandler? MarqueeCompleted;
26+
27+
private void Marquee_Loaded(object sender, RoutedEventArgs e)
28+
{
29+
// While loaded, detach the loaded event and attach the unloaded event
30+
this.Loaded -= this.Marquee_Loaded;
31+
this.Unloaded += Marquee_Unloaded;
32+
33+
// Attach other events
34+
if (_marqueeContainer is not null)
35+
{
36+
_marqueeContainer.SizeChanged += Container_SizeChanged;
37+
}
38+
39+
if (_marqueeStoryboard is not null)
40+
{
41+
_marqueeStoryboard.Completed += StoryBoard_Completed;
42+
}
43+
}
2644

2745
private void Marquee_Unloaded(object sender, RoutedEventArgs e)
2846
{
47+
// Restore the loaded event and detach the unloaded event
48+
this.Loaded += Marquee_Loaded;
2949
this.Unloaded -= Marquee_Unloaded;
3050

3151
if (_marqueeContainer is not null)

components/Marquee/src/Marquee.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ protected override void OnApplyTemplate()
7373
_marqueeContainer.SizeChanged += Container_SizeChanged;
7474

7575
// Swapping tabs in TabView caused errors where the control would unload and never reattach events.
76-
// Hotfix: Don't detach events. This should be fine because the GC will handle it.
77-
// However, more research is required.
78-
//Unloaded += Marquee_Unloaded;
76+
// Hotfix: Track the loaded event. This should be fine because the GC will handle detaching the Loaded
77+
// event on disposal. However, more research is required
78+
Loaded += this.Marquee_Loaded;
7979

8080
VisualStateManager.GoToState(this, GetVisualStateName(Direction), false);
8181
VisualStateManager.GoToState(this, GetVisualStateName(Behavior), false);

0 commit comments

Comments
 (0)