本文介绍了停止在WPF的动画,动态加载的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序的主窗口:

In my main window of the application:

public MainWindow() {
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 - 100;
    this.Top = corner.Y - this.ActualHeight;
}));

NotificationWindow nw1 = new NotificationWindow();
spNotifiers.Children.Add(nw1);

}

我的通知是这样的:

<UserControl 
x:Class="NotificationWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="Transparent" MouseEnter="UserControl_MouseEnter">

<Grid RenderTransformOrigin="0,1">

    <Border BorderThickness="1" Background="Beige" BorderBrush="Black" CornerRadius="10">
        <StackPanel Margin="20">
            <TextBlock TextWrapping="Wrap" Margin="5"> 
      <Bold>Besked fra podio</Bold><LineBreak /><LineBreak /> 
      Her skal der være en podio notification!
            </TextBlock>
            <CheckBox Content="Check check" Margin="5 5 0 5" />
            <Button Content="Klik på mig" HorizontalAlignment="Center" />
        </StackPanel>
    </Border>

    <!-- Animation -->
    <Grid.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard>
                <Storyboard x:Name="sbMain">
                    <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>
    </Grid.Triggers>

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

</Grid>

private void UserControl_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e) {
sbMain.Stop(); // not working

}

从通知我有一个mouse_anter事件,应停止动画/故事板,但无论我怎么努力,我不能使动画停止。

From the notifier I have a mouse_anter event, that should stop the animation/storyboard, but no matter what I try I can't make the animation stop.

任何想法?

推荐答案

我已经找到了解决方案,但它需要删除触发。

I have found a solution but it requires to remove the Trigger.

第一步:把你的动画为UserControl.Resources,并给它的属性为X:键= sbMain

Step1: Put your animation into "UserControl.Resources" and give it the attribute "x:Key=sbMain"

第二步:添加一个事件,用户控件加载

Step2: Add an event to UserControl Loaded

第三步:在变化的背后codesbMain.Stop();为((故事板)FindResource(sbMain))停止();,你可能需要添加一些using指令

Step3: In code behind change "sbMain.Stop();" to "((Storyboard)FindResource("sbMain")).Stop();", may you have to add some using directives.

第四步:。()开始()来添加比如LoadEvent(故事板)FindResource(sbMain);

Step4: To the LoadEvent add "((Storyboard)FindResource("sbMain")).Begin();"

结果看起来是这样的:

<UserControl x:Class="NotificationWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="Transparent" MouseEnter="UserControl_MouseEnter" Width="300" Height="200" Loaded="UserControl_Loaded">
<UserControl .Resources>
    <Storyboard x:Key="sbMain">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)" Storyboard.TargetName="AnimatedGrid">
            <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
            <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="AnimatedGrid">
            <SplineDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
            <SplineDoubleKeyFrame KeyTime="0:0:4" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</UserControl .Resources>


<Grid RenderTransformOrigin="0,1" x:Name="AnimatedGrid">

    <Border BorderThickness="1" Background="Beige" BorderBrush="Black" CornerRadius="10">
        <StackPanel Margin="20">
            <TextBlock TextWrapping="Wrap" Margin="5"> 
  <Bold>Besked fra podio</Bold><LineBreak /><LineBreak /> 
  Her skal der være en podio notification!
            </TextBlock>
            <CheckBox Content="Check check" Margin="5 5 0 5" />
            <Button Content="Klik på mig" HorizontalAlignment="Center" />
        </StackPanel>
    </Border>

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

</Grid>

在code背后:

private void UserControl_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
    ((Storyboard)FindResource("sbMain")).Stop();
}

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
        ((Storyboard)FindResource("sbMain")).Begin();
}

这code在我的情况下工作。

this code worked in my case.

这篇关于停止在WPF的动画,动态加载的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 00:56