Dick Dick - 13 days ago 8
C# Question

Toaster Window does not close in Task Manager

I use a toaster window , this is the main part of the XAML:

<Grid.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard Completed="Storyboard_Completed">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)" Duration="0:0:10" Completed="DoubleAnimationCompleted">
<SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
<SplineDoubleKeyFrame KeyTime="0:0:12" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>


Basically it's called with a Show method like this:

public new void Show()
{
this.Topmost = true;
base.Show();

this.Owner = System.Windows.Application.Current.MainWindow;
this.Closed += this.NotificationWindowClosed;
var workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;

this.Left = workingArea.Right - this.ActualWidth;
double top = workingArea.Bottom - this.ActualHeight;
foreach (Window window in System.Windows.Application.Current.Windows)
{
string windowName = window.GetType().Name;

if (windowName.Equals("NotificationWindow") && window != this)
{
window.Topmost = true;
top = window.Top - window.ActualHeight;
}
}

this.Top = top;
}


The problem is that the Notifications popup remain as a subwindow in the Task Manager. Every time a toaster windows opens (and closes again) an entry is added. In the XAML I already added Completed="DoubleAnimationCompleted" and (following another post in Stackoverflow) a Storyboard Completed="Storyboard_Completed">. Both methods are called and exectute this.Close() but the popups do not diasappear from the taskbar. So this.Close (in whatever way called) seems not to clear these entries.

What can I do to change that?

Answer

I finally found the answer from a sample posted by "CrashproofCode" in this WPF 'Toaster' Popup - How to close?. After comparing his code to mine I found 2 differences which caused my code to remain in the taskbar:

WindowStyle="None" AllowsTransparency="True" Background="Transparent" Closing="Window_Closing"

The Window_Closing was called and apparently caused the problem:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (!closeCompleted)
    {
        e.Cancel = true;
    }
}

He also had in his XAML:

ShowInTaskbar="False"

and that alone is sufficient to prevent that too.