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

[英]C# WPF pausing an animated window with storyboard


I am making a C# wpf application with Visual Studio 2012. Here is the toast like notification. It is animated to go off by 4 seconds. It I get the mouse over this I want to pause the animation.

我正在使用Visual Studio 2012创建一个C#wpf应用程序。这是像通知一样的toast。它的动画效果会持续4秒。它我把鼠标放在这个我想暂停动画。

How can I achieve that ?

我怎样才能做到这一点?

<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>

Code behind this

代码背后的这个

 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 个解决方案

#1


1  

You can use the PauseStoryboard Class and the UIElement.MouseEnter Event to pause a running Animation. Equally, if you want the Animation to resume when the mouse is no longer over the control, then you can use the ResumeStoryboard Class and the UIElement.MouseLeave Event. Here is a simple example to demonstrate:

您可以使用PauseStoryboard类和UIElement.MouseEnter事件来暂停正在运行的动画。同样,如果您希望在鼠标不再在控件上时恢复动画,则可以使用ResumeStoryboard类和UIElement.MouseLeave事件。这是一个简单的例子来演示:

<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>
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/07/16/ef768f7706ac61e4ebe6910c7868e736.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告