1
我正在试图制作一个通知弹出窗口,以显示某些事件。每件事情都进展顺利,但问题是我无法关闭该通知弹出窗口,因为它仍然保留在任务栏中,但在显示几秒钟后隐藏。任何帮助将不胜感激。如何在WPF中通过触发器关闭窗口
XAML文件如下:
<Window
x:Class="SQLExample.NotificationWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Notification Popup" Width="300" SizeToContent="Height"
WindowStyle="None" AllowsTransparency="True" Background="Transparent">
<Grid RenderTransformOrigin="0,1" x:Name="NotificationWindowsss">
<!-- Notification area -->
<Border BorderThickness="1" Background="White" BorderBrush="Black" CornerRadius="0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="4*"/>
<RowDefinition Height="7"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Border CornerRadius="0,0,0,0" Background="SkyBlue">
<Grid>
<Button HorizontalAlignment="Right" Margin="5,5,5,5" Click="Button_Click" Grid.Row="0" Height="15" Width="15" Foreground="White">
<Button.Template>
<ControlTemplate>
<Grid>
<Ellipse>
<Ellipse.Fill>
<ImageBrush ImageSource="Images\Close-icon.png"/>
</Ellipse.Fill>
</Ellipse>
<ContentPresenter Content="x" HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
<TextBlock TextWrapping="Wrap" Margin="10" Grid.Row="0" HorizontalAlignment="Left">
<Bold>Notification data</Bold>
</TextBlock>
</Grid>
</Border>
</Grid>
<StackPanel Margin="10" Grid.Row="1">
<TextBlock TextWrapping="Wrap">
Something just happened and you are being notified of it.
</TextBlock>
</StackPanel>
<Grid Grid.Row="2" Background="SkyBlue"/>
</Grid>
</Border>
<!-- Animation -->
<Grid.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)">
<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:4" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="0:0:7" Value="0"/>
<SplineDoubleKeyFrame KeyTime="0:0:0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Grid.Triggers>
<Grid.RenderTransform>
<ScaleTransform ScaleY="1" />
</Grid.RenderTransform>
</Grid>
</Window>
和xaml.cs在这里。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace SQLExample
{
public partial class NotificationWindow : Window
{
public NotificationWindow()
{
InitializeComponent();
Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
{
//var workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
var workingArea = System.Windows.SystemParameters.WorkArea;
var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
var corner = transform.Transform(new Point(workingArea.Right, workingArea.Bottom));
this.Left = corner.X - this.ActualWidth-10;
this.Top = corner.Y - this.ActualHeight;
}));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
事情我想要做的是,当通知弹出窗口消失或技术意义上得到“透明度”等于零,那么就应该关闭自己,我是想通过触发器来做到这一点,但是,解决方案无法得到。引导我如何做到这一点或其他简单的方法来做到这一点。
预先感谢您。
如果你想要一个纯粹的xaml解决方案,[见这里](http://stackoverflow.com/questions/16573960/how-to-bind-to-windows-close-button-the-x-button)(这不是公认的答案,但工作原理也是如此。) –
非常感谢您的回复,但我认为您告诉我的解决方案与按钮按下相关,我的意思是用户必须按按钮关闭窗口,而且我已经在做我的通知关闭按钮。我想要自动关闭窗口,就像我想到的那样,当窗口的**不透明**变成零**时,窗口通过在不透明度等级零上触发触发器自动关闭。 –