Animation on Visiblity change: property has already been registered in 'FrameworkElement'
I am using this code to set animation when visibility changes.
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;
}
In xaml I need to install: VisibilityAnimation.AnimationType="Fade"
The animation works fine, but the problem is I have a mistake in the title.
How can I fix this?
Stack overflow wants more details to insert this code, but all of this .. write this phrase, hoping I can insert it.
source to share
According to MSDN ( http://msdn.microsoft.com/en-us/library/ms754209%28v=vs.110%29.aspx ) OverrideMetadata (related to AddOwner) should only be called from a static constructor of a type that has this property Metadata overridden. I assume this rule is the same for the AddOwner method of the dependency property.
By the way, do you really need such an application? Based on the code you have, it looks like you are actually looking for a visibility change notification for the FrameworkElement that has your attached property assigned. You can achieve this in the following way. DependencyPropertyDescriptor.FromProperty (UIElement.VisibilityProperty, typeof (FrameworkElement)). AddValueChange (frameworkElement, callback)
But since what you are looking for is an application using a wide hook to callback the visibility value for FrameworkElements, you can do something like this
static VisibilityAnimation()
{
// Here we "register" on Visibility property "before change" event
var desc = DependencyPropertyDescriptor.FromProperty(UIElement.VisibilityProperty, typeof(FrameworkElement));
desc.DesignerCoerceValueCallback += CoerceVisibility;
By the way, there is a high probability that the FrameworkElements that this property is bound to will not get garbage collected, since they are collected in the _hookedElements dictionary until the AnimationType property changes to AnimationType.None.
source to share
In your static constructor, change the owner type to VisibilityAnimation
class
static VisibilityAnimation()
{
// Here we "register" on Visibility property "before change" event
UIElement.VisibilityProperty.AddOwner(
typeof(VisibilityAnimation),
new FrameworkPropertyMetadata(
Visibility.Visible,
VisibilityChanged,
CoerceVisibility));
}
source to share