2009-09-23 21 views
7

में एनिमेट मार्जिन चेंज मैं सिल्वरलाइट में सीमा का आकार बदल रहा हूं, हालांकि मुझे इसके चारों ओर मार्जिन धीरे-धीरे हटाने की आवश्यकता है (वर्तमान में 50)। मिश्रण मार्जिन परिवर्तन के लिए एक ट्विन उत्पन्न नहीं करता प्रतीत होता है - यह सिर्फ एक ही समय में 50 से 0 तक कूदता है। क्या इसको हासिल करने के लिए कोई रास्ता है?सिल्वरलाइट

उत्तर

8

समस्या यह है कि मार्जिन वास्तव में "System.Windows.hickness" प्रकार का प्रकार है जो एक निर्भरता वस्तु नहीं है, इस प्रकार बाएं, ऊपर, दाएं, और नीचे निर्भरता गुण नहीं हैं और इस प्रकार डबलएनीमेशन (जिसका उपयोग करके एनिमेटेड नहीं किया जा सकता है) tweening के लिए अनुमति देता है)।

मार्जिन को एनिमेट करने के लिए क्या उपयोग किया जाता है एक ऑब्जेक्टएनीमेशन जो ट्विन नहीं करता है। यही कारण है कि आप मार्जिन कूद को अपने मूल स्थान से अपने नए स्थान पर देखते हैं। एक और आम उदाहरण के रूप में, वही होता है जब आप दृश्यमान और संक्षिप्त के बीच दृश्यता संपत्ति को एनिमेट करने का प्रयास करते हैं।

आपको या तो मार्जिन को एनिमेट करने या मोटाई ऑब्जेक्ट्स के लिए अपने स्वयं के एनिमेशन प्रकार को लागू करने के लिए टाइमर आधारित एनीमेशन करने की आवश्यकता होगी।

0

Here is an updated version कि आप XAML

भीतर
using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

namespace NiceCards.Animations 
{ 
    public class ThicknessAnimationX 
    { 
     public static readonly DependencyProperty ElementProperty = DependencyProperty.RegisterAttached("Element", typeof(DependencyObject), typeof(DoubleAnimation), new PropertyMetadata(new PropertyChangedCallback(OnElementPropertyChanged))); 

     // The time along the animation from 0-1 
     public static DependencyProperty TimeProperty = DependencyProperty.RegisterAttached("Time", typeof(double), typeof(DoubleAnimation), new PropertyMetadata(OnTimeChanged)); 

     // The object being animated 
     public static DependencyProperty TargetProperty = DependencyProperty.RegisterAttached("Target", typeof(DependencyObject), typeof(ThicknessAnimationX), null); 
     public static DependencyProperty TargetPropertyProperty = DependencyProperty.RegisterAttached("TargetProperty", typeof(DependencyProperty), typeof(DependencyObject), null); 

     public static readonly DependencyProperty FromProperty = DependencyProperty.RegisterAttached("From", typeof(Thickness), typeof(DoubleAnimation), null); 
     public static readonly DependencyProperty ToProperty = DependencyProperty.RegisterAttached("To", typeof(Thickness), typeof(DoubleAnimation), null); 

     public static void SetElement(DependencyObject o, DependencyObject value) 
     { 
      o.SetValue(ElementProperty, value); 
     } 

     public static DependencyObject GetElement(DependencyObject o) 
     { 
      return (DependencyObject)o.GetValue(ElementProperty); 
     } 

     private static void OnElementPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      if (e.NewValue != null) 
      { 
       DoubleAnimation doubleAnimation = (DoubleAnimation)d; 

       doubleAnimation.SetValue(TargetProperty, e.NewValue); 
       doubleAnimation.From = 0; 
       doubleAnimation.To = 1; 
       doubleAnimation.SetValue(TargetPropertyProperty, FrameworkElement.MarginProperty); 
       Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(ThicknessAnimationX.Time)")); 
       Storyboard.SetTarget(doubleAnimation, doubleAnimation); 
      } 
     } 


     private static void OnTimeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
     { 
      DoubleAnimation animation = (DoubleAnimation)sender; 
      double time = GetTime(animation); 
      Thickness from = (Thickness)sender.GetValue(FromProperty); 
      Thickness to = (Thickness)sender.GetValue(ToProperty); 
      DependencyProperty targetProperty = (DependencyProperty)sender.GetValue(TargetPropertyProperty); 
      DependencyObject target = (DependencyObject)sender.GetValue(TargetProperty); 
      target.SetValue(targetProperty, new Thickness((to.Left - from.Left) * time + from.Left, 
                  (to.Top - from.Top) * time + from.Top, 
                  (to.Right - from.Right) * time + from.Right, 
                  (to.Bottom - from.Bottom) * time + from.Bottom)); 
     } 

     public static double GetTime(DoubleAnimation animation) 
     { 
      return (double)animation.GetValue(TimeProperty); 
     } 

     public static void SetTime(DoubleAnimation animation, double value) 
     { 
      animation.SetValue(TimeProperty, value); 
     } 

     public static Thickness GetFrom(DoubleAnimation animation) 
     { 
      return (Thickness)animation.GetValue(FromProperty); 
     } 

     public static void SetFrom(DoubleAnimation animation, Thickness value) 
     { 
      animation.SetValue(FromProperty, value); 
     } 

     public static Thickness GetTo(DoubleAnimation animation) 
     { 
      return (Thickness)animation.GetValue(ToProperty); 
     } 

     public static void SetTo(DoubleAnimation animation, Thickness value) 
     { 
      animation.SetValue(ToProperty, value); 
     } 
    } 
} 

से चेतन करने के लिए और फिर आप XAML में ऐसा कर सकते हैं

<VisualStateManager.VisualStateGroups> 
    <VisualStateGroup x:Name="Positions"> 
     <VisualStateGroup.Transitions> 
      <VisualTransition GeneratedDuration="0:0:0.2"/> 
     </VisualStateGroup.Transitions> 
     <VisualState x:Name="Left">      
      <Storyboard> 
       <DoubleAnimation Duration="0:0:0.3" NiceCards:ThicknessAnimationX.To="0,0,0,0" NiceCards:ThicknessAnimationX.Element="{Binding ElementName=rectangle1}" Storyboard.TargetName="rectangle1" Storyboard.TargetProperty="Opacity"/> 
      </Storyboard>      
     </VisualState> 
     <VisualState x:Name="Right">      
      <Storyboard> 
       <DoubleAnimation Duration="0:0:0.3" NiceCards:ThicknessAnimationX.To="0,200,0,0" NiceCards:ThicknessAnimationX.Element="{Binding ElementName=rectangle1}" Storyboard.TargetName="rectangle1" Storyboard.TargetProperty="Opacity"/> 
      </Storyboard>      
     </VisualState> 
    </VisualStateGroup> 
</VisualStateManager.VisualStateGroups> 
<Rectangle Height="100" HorizontalAlignment="Left" Margin="23,25,0,0" x:Name="rectangle1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="200" Fill="#FF1BAA00"/> 

नोट की अनुमति देता है कि अगर आप एक्सएएमएल में डबल एनीमेशन के लिए लक्ष्य प्रॉपर्टी सेट नहीं करते हैं, तो आप ब्लेंड में नियंत्रण/पेज प्रदर्शित नहीं कर पाएंगे। इसे ठीक करने के लिए, केवल नकली लक्ष्य संपत्ति जोड़ें (ऊपर दिए गए कोड में मैंने अस्पष्टता संपत्ति जो डबल मान है) जोड़ा है, और यह रनटाइम पर ओवरराइड हो जाएगा, फिर भी

+0

मैंने आपके अनुसार XAML में इसका उपयोग करने का प्रयास किया है उदाहरण और मुझे एसएल 5 में त्रुटियों की धारा मिल रही है। मैंने एक 'xmlns: someName' घोषणा को जोड़ा है, लेकिन ऐसा लगता है कि एक्सएएमएल नहीं जानता कि 'कुछ नाम: मोटाईएनीमेशन एक्स' संपत्ति क्या है। – Shaamaan