首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可见变化的动画:属性已由“FrameworkElement”注册

可见变化的动画:属性已由“FrameworkElement”注册
EN

Stack Overflow用户
提问于 2014-08-23 08:52:37
回答 2查看 1.2K关注 0票数 3

我使用这段代码来设置一个可见性变化的动画。

代码语言:javascript
复制
public class VisibilityAnimation : DependencyObject
{
    public enum AnimationType
    {
        None,
        Fade
    }

    private const int AnimationDuration = 1000;

    private static readonly Dictionary<FrameworkElement, bool> _hookedElements =
        new Dictionary<FrameworkElement, bool>();

    public static AnimationType GetAnimationType(DependencyObject obj)
    {
        return (AnimationType)obj.GetValue(AnimationTypeProperty);
    }

    public static void SetAnimationType(DependencyObject obj, AnimationType value)
    {
        obj.SetValue(AnimationTypeProperty, value);
    }

    public static readonly DependencyProperty AnimationTypeProperty =
        DependencyProperty.RegisterAttached(
            "AnimationType",
            typeof(AnimationType),
            typeof(VisibilityAnimation),
            new FrameworkPropertyMetadata(AnimationType.None,
                new PropertyChangedCallback(OnAnimationTypePropertyChanged)));
    private static void OnAnimationTypePropertyChanged(
        DependencyObject dependencyObject,
        DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement frameworkElement = dependencyObject as FrameworkElement;

        if (frameworkElement == null)
        {
            return;
        }

        // If AnimationType is set to True on this framework element, 
        if (GetAnimationType(frameworkElement) != AnimationType.None)
        {
            // Add this framework element to hooked list
            HookVisibilityChanges(frameworkElement);
        }
        else
        {
            // Otherwise, remove it from the hooked list
            UnHookVisibilityChanges(frameworkElement);
        }
    }
    private static void HookVisibilityChanges(FrameworkElement frameworkElement)
    {
        _hookedElements.Add(frameworkElement, false);
    }
    private static void UnHookVisibilityChanges(FrameworkElement frameworkElement)
    {
        if (_hookedElements.ContainsKey(frameworkElement))
        {
            _hookedElements.Remove(frameworkElement);
        }
    }
    static VisibilityAnimation()
    {
        // Here we "register" on Visibility property "before change" event
        UIElement.VisibilityProperty.AddOwner(
            typeof(FrameworkElement),
            new FrameworkPropertyMetadata(
                Visibility.Visible,
                VisibilityChanged,
                CoerceVisibility));

    }
    private static void VisibilityChanged(
        DependencyObject dependencyObject,
        DependencyPropertyChangedEventArgs e)
    {
        // Ignore
    }
    private static object CoerceVisibility(
        DependencyObject dependencyObject,
        object baseValue)
    {
        // Make sure object is a framework element
        FrameworkElement frameworkElement = dependencyObject as FrameworkElement;
        if (frameworkElement == null)
        {
            return baseValue;
        }

        // Cast to type safe value
        Visibility visibility = (Visibility)baseValue;

        // If Visibility value hasn't change, do nothing.
        // This can happen if the Visibility property is set using data binding 
        // and the binding source has changed but the new visibility value 
        // hasn't changed.
        if (visibility == frameworkElement.Visibility || visibility == Visibility.Collapsed) //Aggiungo da cri..x fare l'effetto solo sul fade in
        {
            return baseValue;
        }

        // If element is not hooked by our attached property, stop here
        if (!IsHookedElement(frameworkElement))
        {
            return baseValue;
        }

        // Update animation flag
        // If animation already started, don't restart it (otherwise, infinite loop)
        if (UpdateAnimationStartedFlag(frameworkElement))
        {
            return baseValue;
        }

        // If we get here, it means we have to start fade in or fade out animation. 
        // In any case return value of this method will be Visibility.Visible, 
        // to allow the animation.
        DoubleAnimation doubleAnimation = new DoubleAnimation
        {
            Duration = new Duration(TimeSpan.FromMilliseconds(AnimationDuration))
        };

        // When animation completes, set the visibility value to the requested 
        // value (baseValue)
        doubleAnimation.Completed += (sender, eventArgs) =>
        {
            if (visibility == Visibility.Visible)
            {
                // In case we change into Visibility.Visible, the correct value 
                // is already set, so just update the animation started flag
                UpdateAnimationStartedFlag(frameworkElement);
            }
            else
            {
                // This will trigger value coercion again 
                // but UpdateAnimationStartedFlag() function will reture true 
                // this time, thus animation will not be triggered. 
                if (BindingOperations.IsDataBound(frameworkElement,
                    UIElement.VisibilityProperty))
                {
                    // Set visiblity using bounded value
                    Binding bindingValue =
                        BindingOperations.GetBinding(frameworkElement,
                            UIElement.VisibilityProperty);
                    BindingOperations.SetBinding(frameworkElement,
                        UIElement.VisibilityProperty, bindingValue);
                }
                else
                {
                    // No binding, just assign the value
                    frameworkElement.Visibility = visibility;
                }
            }
        };

        if (visibility == Visibility.Collapsed || visibility == Visibility.Hidden)
        {
            // Fade out by animating opacity
            doubleAnimation.From = 1.0;
            doubleAnimation.To = 0.0;
        }
        else
        {
            // Fade in by animating opacity
            doubleAnimation.From = 0.0;
            doubleAnimation.To = 1.0;
        }

        // Start animation
        frameworkElement.BeginAnimation(UIElement.OpacityProperty, doubleAnimation);

        // Make sure the element remains visible during the animation
        // The original requested value will be set in the completed event of 
        // the animation
        return Visibility.Visible;
    }
    private static bool IsHookedElement(FrameworkElement frameworkElement)
    {
        return _hookedElements.ContainsKey(frameworkElement);
    }
    private static bool UpdateAnimationStartedFlag(FrameworkElement frameworkElement)
    {
        bool animationStarted = (bool)_hookedElements[frameworkElement];
        _hookedElements[frameworkElement] = !animationStarted;

        return animationStarted;
    }

在xaml中,我需要设置:VisibilityAnimation.AnimationType="Fade"

动画工作正常,但问题是我在标题中出现了错误。

我怎么才能解决这个问题?

堆栈溢出需要更多详细信息来插入这段代码,但仅此而已。写这个短语,希望我能把它插入。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-03 23:48:48

根据MSDN (http://msdn.microsoft.com/en-us/library/ms754209%28v=vs.110%29.aspx),OverrideMetadata (与AddOwner相关)只应从其属性元数据覆盖的类型的静态构造函数中调用。对于依赖项属性的AddOwner方法,我假设此规则是相同的。

顺便问一下,您真的需要这样的应用程序宽钩子吗?基于您所拥有的代码,看起来您真正要寻找的是分配给您的附加属性的FrameworkElement的可见性更改通知。您可以通过以下方式实现这一点。DependencyPropertyDescriptor.FromProperty(UIElement.VisibilityProperty,typeof(FrameworkElement)).AddValueChange(frameworkElement,回调)

但是,由于您正在寻找的是FrameworkElements的可见性核心值回调的应用程序宽挂钩,所以您可以这样做

代码语言:javascript
复制
    static VisibilityAnimation()
    {
        // Here we "register" on Visibility property "before change" event
        var desc = DependencyPropertyDescriptor.FromProperty(UIElement.VisibilityProperty, typeof(FrameworkElement));
        desc.DesignerCoerceValueCallback += CoerceVisibility;

顺便说一句,这个属性被链接到的FrameworkElements很有可能不会被垃圾收集,因为它们被捆绑在_hookedElements字典中,直到AnimationType属性更改为AnimationType.None。

票数 4
EN

Stack Overflow用户

发布于 2014-08-28 09:54:33

在静态构造函数中,将所有者类型更改为VisibilityAnimation类。

代码语言:javascript
复制
static VisibilityAnimation()
{
    // Here we "register" on Visibility property "before change" event
    UIElement.VisibilityProperty.AddOwner(
        typeof(VisibilityAnimation),
        new FrameworkPropertyMetadata(
            Visibility.Visible,
            VisibilityChanged,
            CoerceVisibility));

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25460211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档