WPF动画无法在运行时播放

时间:2015-08-06 15:53:30

标签: wpf xaml animation designer

我有一个在设计器中完美运行的进度指示器,但在调试时无法移动一英寸。相同的代码(除了名称空间当然)对我的另一个项目工作正常,但除了那个之外,我无法将该专长复制到任何其他项目上。就目前而言,动画正在运行,因为点“闪烁”(重置动画的位置),但它们不会像设计师那样移动。

这是一张应该是什么样子的图片:http://oi62.tinypic.com/2u5ukvm.jpg

以下是目前的图片:http://oi58.tinypic.com/2n6g9rl.jpg

ProgressIndicator.xaml

<UserControl x:Class="Iris.UI.ProgressIndicator"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:UI="clr-namespace:Iris.UI"
             x:Name="progressIndicator"
             mc:Ignorable="d">
    <UserControl.Resources>
        <UI:WidthPercentageConverter x:Key="WidthPercentageConverter" />
        <CircleEase EasingMode="EaseIn" x:Key="ProgressBarEaseIn"/>

        <CircleEase EasingMode="EaseIn" x:Key="ProgressBarEaseOut" />

        <Storyboard RepeatBehavior="Forever" Duration="00:00:04.4" x:Key="animate">
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00.0" Storyboard.TargetProperty="X" Storyboard.TargetName="R1TT">
                <LinearDoubleKeyFrame KeyTime="00:00:00.0" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=0}" />
                <EasingDoubleKeyFrame KeyTime="00:00:00.5" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=.32}" EasingFunction="{StaticResource ProgressBarEaseOut}" />
                <LinearDoubleKeyFrame KeyTime="00:00:02.0" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=.65}" />
                <EasingDoubleKeyFrame KeyTime="00:00:02.5" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=1}" EasingFunction="{StaticResource ProgressBarEaseIn}" />
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00.2" Storyboard.TargetProperty="X" Storyboard.TargetName="R2TT">
                <LinearDoubleKeyFrame KeyTime="00:00:00.0" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=0}" />
                <EasingDoubleKeyFrame KeyTime="00:00:00.5" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=.32}" EasingFunction="{StaticResource ProgressBarEaseOut}" />
                <LinearDoubleKeyFrame KeyTime="00:00:02.0" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=.65}" />
                <EasingDoubleKeyFrame KeyTime="00:00:02.5" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=1}" EasingFunction="{StaticResource ProgressBarEaseIn}" />
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00.4" Storyboard.TargetProperty="X" Storyboard.TargetName="R3TT">
                <LinearDoubleKeyFrame KeyTime="00:00:00.0" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=0}" />
                <EasingDoubleKeyFrame KeyTime="00:00:00.5" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=.32}" EasingFunction="{StaticResource ProgressBarEaseOut}" />
                <LinearDoubleKeyFrame KeyTime="00:00:02.0" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=.65}" />
                <EasingDoubleKeyFrame KeyTime="00:00:02.5" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=1}" EasingFunction="{StaticResource ProgressBarEaseIn}" />
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00.6" Storyboard.TargetProperty="X" Storyboard.TargetName="R4TT">
                <LinearDoubleKeyFrame KeyTime="00:00:00.0" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=0}" />
                <EasingDoubleKeyFrame KeyTime="00:00:00.5" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=.32}" EasingFunction="{StaticResource ProgressBarEaseOut}" />
                <LinearDoubleKeyFrame KeyTime="00:00:02.0" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=.65}" />
                <EasingDoubleKeyFrame KeyTime="00:00:02.5" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=1}" EasingFunction="{StaticResource ProgressBarEaseIn}" />
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00.8" Storyboard.TargetProperty="X" Storyboard.TargetName="R5TT">
                <LinearDoubleKeyFrame KeyTime="00:00:00.0" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=0}" />
                <EasingDoubleKeyFrame KeyTime="00:00:00.5" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=.32}" EasingFunction="{StaticResource ProgressBarEaseOut}" />
                <LinearDoubleKeyFrame KeyTime="00:00:02.0" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=.65}" />
                <EasingDoubleKeyFrame KeyTime="00:00:02.5" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},AncestorLevel=1}, Path=ActualWidth, Converter={StaticResource WidthPercentageConverter}, ConverterParameter=1}" EasingFunction="{StaticResource ProgressBarEaseIn}" />
            </DoubleAnimationUsingKeyFrames>

            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00.0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="R1">
                <DiscreteDoubleKeyFrame KeyTime="0" Value="1" />
                <DiscreteDoubleKeyFrame KeyTime="00:00:02.5" Value="0" />
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00.2" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="R2">
                <DiscreteDoubleKeyFrame KeyTime="0" Value="1" />
                <DiscreteDoubleKeyFrame KeyTime="00:00:02.5" Value="0" />
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00.4" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="R3">
                <DiscreteDoubleKeyFrame KeyTime="0" Value="1" />
                <DiscreteDoubleKeyFrame KeyTime="00:00:02.5" Value="0" />
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00.6" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="R4">
                <DiscreteDoubleKeyFrame KeyTime="0" Value="1" />
                <DiscreteDoubleKeyFrame KeyTime="00:00:02.5" Value="0" />
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00.8" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="R5">
                <DiscreteDoubleKeyFrame KeyTime="0" Value="1" />
                <DiscreteDoubleKeyFrame KeyTime="00:00:02.5" Value="0" />
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </UserControl.Resources>
    <UserControl.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource animate}"/>
        </EventTrigger>
    </UserControl.Triggers>
    <Border x:Name="IndeterminateRoot" Visibility="Visible"  VerticalAlignment="Stretch">
        <Grid VerticalAlignment="Stretch" Margin="0" HorizontalAlignment="Left">
            <Rectangle Fill="{DynamicResource MetroStyle}" VerticalAlignment="Stretch" IsHitTestVisible="False" Width="4" x:Name="R1" HorizontalAlignment="Left" >
                <Rectangle.RenderTransform>
                    <TranslateTransform x:Name="R1TT" />
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle Fill="{DynamicResource MetroStyle}" VerticalAlignment="Stretch" IsHitTestVisible="False" Width="4" x:Name="R2" HorizontalAlignment="Left" >
                <Rectangle.RenderTransform>
                    <TranslateTransform x:Name="R2TT" />
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle Fill="{DynamicResource MetroStyle}" VerticalAlignment="Stretch" IsHitTestVisible="False" Width="4" x:Name="R3" HorizontalAlignment="Left" >
                <Rectangle.RenderTransform>
                    <TranslateTransform x:Name="R3TT" />
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle Fill="{DynamicResource MetroStyle}" VerticalAlignment="Stretch" IsHitTestVisible="False" Width="4" x:Name="R4" HorizontalAlignment="Left" >
                <Rectangle.RenderTransform>
                    <TranslateTransform x:Name="R4TT" />
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle Fill="{DynamicResource MetroStyle}" VerticalAlignment="Stretch" IsHitTestVisible="False" Width="4" x:Name="R5" HorizontalAlignment="Left" >
                <Rectangle.RenderTransform>
                    <TranslateTransform x:Name="R5TT" />
                </Rectangle.RenderTransform>
            </Rectangle>
        </Grid>
    </Border>
</UserControl>

ProgressIndicator.xaml.cs

using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Iris.UI
{
    public class WidthPercentageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var percentage = Double.Parse(parameter.ToString(), new CultureInfo("en-US"));
            return ((double)value) * percentage;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public partial class ProgressIndicator
    {
        public ProgressIndicator()
        {
            InitializeComponent();
            this.DataContext = this;
            IsVisibleChanged += (s, e) => ((ProgressIndicator)s).StartStopAnimation();
            DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(VisibilityProperty, GetType());
            dpd.AddValueChanged(this, (s, e) => ((ProgressIndicator)s).StartStopAnimation());
        }

        public static readonly DependencyProperty ProgressColourProperty = DependencyProperty.RegisterAttached("ProgressColour", typeof(Brush), typeof(ProgressIndicator), new UIPropertyMetadata(null));

        public Brush ProgressColour
        {
            get { return (Brush)GetValue(ProgressColourProperty); }
            set { SetValue(ProgressColourProperty, value); }
        }

        private void StartStopAnimation()
        {
            bool shouldAnimate = (Visibility == Visibility.Visible && IsVisible);
            Dispatcher.BeginInvoke(new Action(() =>
            {
                var s = Resources["animate"] as Storyboard;
                if (s != null)
                {
                    if (shouldAnimate)
                        s.Begin();
                    else
                        s.Stop();
                }
            }));
        }
    }
}

0 个答案:

没有答案