如何通过WPF中的触发器关闭窗口

时间:2017-03-08 06:23:45

标签: c# wpf xaml triggers

我正在尝试制作一个我希望在某些事件中显示的通知弹出窗口。一切顺利,但问题是我无法关闭该通知弹出窗口,因为它仍然保留在任务栏中,但在显示几秒钟后隐藏。任何帮助将不胜感激。

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();
        }
    }
}

我想要做的是当通知弹出窗口消失或技术意义上的'不透明度'等于零时它应该关闭它自己并且我试图通过触发器但是无法获得解决方案。指导我如何做到这一点或其他简单方法。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

我在网上查看了一些解决方案之后想出来了,并提出了一个解决方案,可以将一个计时器附加到通知弹出屏幕,当通知弹出窗口显示时,以及当关联的时间过去后,线程将关闭通知弹出窗口,也停止了自己。

我可以按照这种方式工作,但我不知道这个解决方案已经足够好了。