C#WPF暂停动画窗口与故事板

时间:2014-07-16 13:17:46

标签: c# wpf xaml visual-studio-2012 storyboard

我正在使用Visual Studio 2012创建一个C#wpf应用程序。这是像通知一样的toast。它的动画效果是4秒。我将鼠标悬停在此处,我想暂停动画。

我怎样才能做到这一点?

<Window x:Class="Exmaple.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
Title="Notification Popup" Width="300" SizeToContent="Height"
WindowStyle="None" AllowsTransparency="True" Height="Auto"    Background="Transparent">

<Grid x:Name="abc" RenderTransformOrigin="0,1" Height="Auto" Width="300"  Margin="0,0,0,0" MouseEnter="Grid_MouseEnter_1" >

    <!-- Notification area -->
    <Border BorderThickness="1" Background="#FF2D2D30" BorderBrush="Black" CornerRadius="0" Margin="0,0,0,0">
        <!--StackPanel Margin="20"-->
        <TextBlock x:Name="textblocknotify" TextWrapping="Wrap" Height="Auto"   Margin="5" Foreground="White">

            </TextBlock>
            <!--CheckBox Content="Checkable" Margin="5 5 0 5" /-->
            <!--Button Content="Clickable" HorizontalAlignment="Center" /-->
        <!--/StackPanel-->
    </Border>
    <!-- Animation -->



    <Grid.Triggers >
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard>
                <Storyboard  Completed="Storyboard_Completed_1">
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
                        <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                        <SplineDoubleKeyFrame KeyTime="0:0:0.1" Value="1"/>
                    </DoubleAnimationUsingKeyFrames>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
                        <SplineDoubleKeyFrame KeyTime="0:0:4.5" Value="1"/>
                        <SplineDoubleKeyFrame KeyTime="0:0:5" Value="0"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard>
                <Storyboard  Completed="Storyboard_Completed_1">
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
                        <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                        <SplineDoubleKeyFrame KeyTime="0:0:0.1" Value="1"/>
                    </DoubleAnimationUsingKeyFrames>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
                        <SplineDoubleKeyFrame KeyTime="0:0:4.5" Value="1"/>
                        <SplineDoubleKeyFrame KeyTime="0:0:5" Value="0"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Grid.Triggers>



    <Grid.RenderTransform>
        <ScaleTransform ScaleY="1" />
    </Grid.RenderTransform>


</Grid>

此背后的代码

 public partial class Window1 : Window
{
    public Window1(String s)
    {
        InitializeComponent();
        textblocknotify.Text = s;
        Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
        {

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

        //this.Close();
    }

    private void Storyboard_Completed_1(object sender, EventArgs e)
    {
        this.Close();
    }

    private void Grid_MouseEnter_1(object sender, MouseEventArgs e)
    {
       //don't know what to do   

    }

1 个答案:

答案 0 :(得分:1)

您可以使用PauseStoryboard ClassUIElement.MouseEnter Event暂停正在运行的Animation。同样,如果您希望在鼠标不再位于控件之外时恢复Animation,则可以使用ResumeStoryboard ClassUIElement.MouseLeave Event。这是一个简单的例子来说明:

<Button Content="Click Me">
    <Button.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Name="OpacityStoryboard">
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="(UIElement.Opacity)"
                        From="0" To="1" RepeatBehavior="Forever" AutoReverse="True" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="UIElement.MouseEnter">
            <PauseStoryboard BeginStoryboardName="OpacityStoryboard" />
        </EventTrigger>
        <EventTrigger RoutedEvent="UIElement.MouseLeave">
            <ResumeStoryboard BeginStoryboardName="OpacityStoryboard" />
        </EventTrigger>
    </Button.Triggers>
</Button>