【发布时间】:2018-12-17 20:07:45
【问题描述】:
我想在屏幕的右下角显示一个 WPF 幻灯片通知。我需要在主程序中多次显示和隐藏通知,所以我不能每次都关闭()和显示()窗口。我必须继续做 Hide() 和 Show()。
IsHitTestVisible 设置为 False,以便我可以单击窗口上的任意位置来关闭和隐藏通知并将另一个窗口置于前台。
我第一次执行窗口动画时,它运行良好。如果我让窗口动画一直运行到最后,那么下次显示幻灯片通知时,动画就完美显示了。
如果我在动画运行完成之前单击窗口,就会出现问题。单击窗口隐藏窗口,然后情节提要设置为 SkipStoryboardtoFill,FillBehavior 设置为停止,以便动画时钟重置为其原始值。但是,下次显示隐藏的通知窗口时,它似乎会记住窗口在隐藏之前的最后一个窗口位置,然后开始动画。所以动画看起来真的很奇怪。我正在尝试弄清楚如何设置动画,以便每次都以相同的方式显示而不会出现此故障。
<Window
x:Class="NotificationWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Notification Popup"
Width="480"
Height="140"
WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"
>
<Grid Name="ToastWindowGrid" RenderTransformOrigin="0,1">
<Border Name="ToastWindowBorder" BorderThickness="0" Background="#333333">
<StackPanel Name="ToastWindowStackPanel" Margin="10" Orientation="Horizontal">
<Image Name="ToastLogo" Width="100" Height="100" Source="D:\Development\resources\WindowsLogo-Blue-x100.png"/>
<StackPanel Name="ToastMessageStackPanel" Width="359">
<TextBox Name="ToastTitleTextBox" Margin="5" MaxWidth="340" Background="#333333" BorderThickness="0" IsReadOnly="True" Foreground="White" FontSize="20" Text="Windows 10 Upgrade" FontWeight="Bold" HorizontalContentAlignment="Center" Width="Auto" HorizontalAlignment="Stretch" IsHitTestVisible="False"/>
<TextBox Name="TotastMessageTextBox" Margin="5" MaxWidth="340" Background="#333333" BorderThickness="0" IsReadOnly="True" Foreground="LightGray" FontSize="16" Text="A Windows upgrade is available. Click to upgrade or schedule update." HorizontalContentAlignment="Left" TextWrapping="Wrap" IsHitTestVisible="False"/>
</StackPanel>
</StackPanel>
</Border>
<Grid.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Name="BeginToastAnimationStoryboard">
<Storyboard Name="ToastAnimationStoryboard" FillBehavior="Stop">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)" FillBehavior="Stop">
<SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" FillBehavior="Stop">
<SplineDoubleKeyFrame KeyTime="0:0:18" Value="1"/>
<SplineDoubleKeyFrame KeyTime="0:0:20" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="FrameworkElement.MouseUp">
<SkipStoryboardToFill BeginStoryboardName="BeginToastAnimationStoryboard"/>
<RemoveStoryboard BeginStoryboardName="BeginToastAnimationStoryboard"/>
</EventTrigger>
</Grid.Triggers>
<Grid.RenderTransform>
<ScaleTransform ScaleY="1" />
</Grid.RenderTransform>
</Grid>
后面有代码:
using System;
using System.Windows;
using System.Windows.Threading;
public partial class NotificationWindow
{
public NotificationWindow()
{
InitializeComponent();
Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
{
var workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
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;
}));
}
}
第一次显示通知时,我使用 Loaded 事件处理程序调用 Begin 方法,并将 isControllable 设置为 True。之后,如果窗口的 Visibility 属性设置为 Visible,我使用 IsVisibleChanged 事件处理程序启动动画。
【问题讨论】: