Skip to content
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

Fix for issue #19 - Memory Leak issues caused by Name or x:Name Area reference #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Notifications.Wpf.Sample/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
<Button Content="Show" Click="Button_Click"/>
<Button Content="Show in window" Click="Button_Click_1" />
</UniformGrid>
<controls:NotificationArea x:Name="WindowArea" Position="TopLeft" MaxItems="3"/>
<controls:NotificationArea AreaName="WindowArea" Position="TopLeft" MaxItems="3"/>
</DockPanel>
</Window>
27 changes: 23 additions & 4 deletions Notifications.Wpf/Controls/NotificationArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@

namespace Notifications.Wpf.Controls
{
public class NotificationArea : Control
public class NotificationArea : Control, IDisposable
{
public string AreaName
{
get { return (string)GetValue(AreaNameProp); }
set { SetValue(AreaNameProp, value); }
}

public static readonly DependencyProperty AreaNameProp =
DependencyProperty.Register("AreaName", typeof(string), typeof(NotificationArea), new PropertyMetadata(string.Empty));


public NotificationPosition Position
Expand All @@ -30,17 +37,23 @@ public int MaxItems
get { return (int)GetValue(MaxItemsProperty); }
set { SetValue(MaxItemsProperty, value); }
}

public static readonly DependencyProperty MaxItemsProperty =
DependencyProperty.Register("MaxItems", typeof(int), typeof(NotificationArea), new PropertyMetadata(int.MaxValue));

private IList _items;

public NotificationArea()
{
this.Unloaded += NotificationArea_Unloaded;
NotificationManager.AddArea(this);
}

private void NotificationArea_Unloaded(object sender, RoutedEventArgs e)
{
Dispose();
}

static NotificationArea()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NotificationArea),
Expand All @@ -64,7 +77,7 @@ public async void Show(object content, TimeSpan expirationTime, Action onClick,
{
Content = content
};

notification.MouseLeftButtonDown += (sender, args) =>
{
if (onClick != null)
Expand Down Expand Up @@ -108,7 +121,7 @@ public async void Show(object content, TimeSpan expirationTime, Action onClick,
}
await Task.Delay(expirationTime);
#endif
notification.Close();
notification.Close();
#if NET40
});
#endif
Expand Down Expand Up @@ -138,6 +151,12 @@ private static void DelayExecute(TimeSpan delay, Action actionToExecute)
}
}
#endif

public void Dispose()
{
this.Unloaded -= NotificationArea_Unloaded;
NotificationManager.RemoveArea(this);
}
}

public enum NotificationPosition
Expand Down
9 changes: 7 additions & 2 deletions Notifications.Wpf/NotificationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,20 @@ public void Show(object content, string areaName = "", TimeSpan? expirationTime
_window.Show();
}

foreach (var area in Areas.Where(a => a.Name == areaName))
foreach (var area in Areas.Where(a => a.AreaName == areaName))
{
area.Show(content, (TimeSpan) expirationTime, onClick, onClose);
area.Show(content, (TimeSpan)expirationTime, onClick, onClose);
}
}

internal static void AddArea(NotificationArea area)
{
Areas.Add(area);
}

internal static void RemoveArea(NotificationArea area)
{
Areas.Remove(area);
}
}
}