【问题标题】:Show and Hide WPF window with slide animation multiple times while showing animation consistently each time多次显示和隐藏带有幻灯片动画的 WPF 窗口,同时每次都一致地显示动画
【发布时间】: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 事件处理程序启动动画。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    将 MouseUp EventTrigger 更改为:

    1. 使用 SeekStoryboard 重置到动画的开头
    2. 使用 PauseStoryboard 停止动画。使用 StopStoryboard 将动画运行到填充状态,这不是您想要的:您希望窗口消失。

    这里是 xaml 的变化:

            <EventTrigger RoutedEvent="FrameworkElement.MouseUp">
                <SeekStoryboard BeginStoryboardName="BeginToastAnimationStoryboard" />
                <PauseStoryboard BeginStoryboardName="BeginToastAnimationStoryboard" />
            </EventTrigger>
    

    重置动画可确保在重新启动动画时,它会从情节提要的开头重新开始。

    【讨论】:

      【解决方案2】:

      我建议为每个通知创建一个新实例。让我解释一下为什么。

      1. 您可以使用配置对象独立配置它们,例如一个可以保持可见 10 秒,然后其余通知默认为 5 秒。
      2. 您可以一次显示多个通知。
      3. 作为窗口的每个通知都会启动它自己的事件循环和线程。

      但是,要使其工作,您必须确保窗口​​非常轻并且可以非常快速地初始化。

      【讨论】:

      • 是的,如果我不明白为什么单击窗口将其关闭,然后将其隐藏,然后再次显示它会导致动画:从它的终点开始,这当然是一个选项,然后闪退到它的起点,然后再回到终点。如果我只是让窗口在不单击它的情况下通过淡出而自行消失,那么下次我显示窗口时动画会正确开始。我觉得我在这里遗漏了一些非常简单的东西。
      • 显示窗口时,首先想到的是重置情节提要。
      猜你喜欢
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多