WPF

2013-10-30 2 views
11

मैं अपने मुख्य परियोजना में seprate पुस्तकालय और XAML कोड में gif animating के लिए इस कोड का उपयोग कर रहा में Gif एनिमेट:WPF

<controls:GifImage GifSource="/project;component/Images/my.gif" Stretch="None" /> 

Gif Animating (seprate फ़ाइल):

public class GifImage : Image 
    { 
     #region Memmbers 

     private GifBitmapDecoder _gifDecoder; 
     private Int32Animation _animation; 
     private bool _isInitialized; 

     #endregion Memmbers 

     #region Properties 

     private int FrameIndex 
     { 
      get { return (int)GetValue(FrameIndexProperty); } 
      set { SetValue(FrameIndexProperty, value); } 
     } 

     private static readonly DependencyProperty FrameIndexProperty = 
     DependencyProperty.Register("FrameIndex", typeof(int), typeof(GifImage), new FrameworkPropertyMetadata(0, new PropertyChangedCallback(ChangingFrameIndex))); 

     private static void ChangingFrameIndex(DependencyObject obj, DependencyPropertyChangedEventArgs ev) 
     { 
      GifImage image = obj as GifImage; 
      image.Source = image._gifDecoder.Frames[(int)ev.NewValue]; 
     } 

     /// <summary> 
     /// Defines whether the animation starts on it's own 
     /// </summary> 
     public bool AutoStart 
     { 
      get { return (bool)GetValue(AutoStartProperty); } 
      set { SetValue(AutoStartProperty, value); } 
     } 

     public static readonly DependencyProperty AutoStartProperty = 
     DependencyProperty.Register("AutoStart", typeof(bool), typeof(GifImage), new UIPropertyMetadata(false, AutoStartPropertyChanged)); 

     private static void AutoStartPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
     { 
      if ((bool)e.NewValue) 
       (sender as GifImage).StartAnimation(); 
     } 

     public string GifSource 
     { 
      get { return (string)GetValue(GifSourceProperty); } 
      set { SetValue(GifSourceProperty, value); } 
     } 

     public static readonly DependencyProperty GifSourceProperty = 
     DependencyProperty.Register("GifSource", typeof(string), typeof(GifImage), new UIPropertyMetadata(string.Empty, GifSourcePropertyChanged)); 

     private static void GifSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
     { 
      // CARLO 20100622: Reinitialize animation everytime image is changed 
      (sender as GifImage).Initialize(); 
     } 

     #endregion Properties 

     #region Private Instance Methods 

     private void Initialize() 
     { 

      _gifDecoder = new GifBitmapDecoder(new Uri(String.Format("pack://application:,,,{0}", this.GifSource)), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); 
      _animation = new Int32Animation(0, _gifDecoder.Frames.Count - 1, new Duration(new TimeSpan(0, 0, 0, _gifDecoder.Frames.Count/10, (int)((_gifDecoder.Frames.Count/10.0 - _gifDecoder.Frames.Count/10) * 1000)))); 
      _animation.RepeatBehavior = RepeatBehavior.Forever; 
      this.Source = _gifDecoder.Frames[0]; 

      _isInitialized = true; 
     } 

     #endregion Private Instance Methods 

     #region Public Instance Methods 

     /// <summary> 
     /// Shows and starts the gif animation 
     /// </summary> 
     public void Show() 
     { 
      this.Visibility = Visibility.Visible; 
      this.StartAnimation(); 
     } 

     /// <summary> 
     /// Hides and stops the gif animation 
     /// </summary> 
     public void Hide() 
     { 
      this.Visibility = Visibility.Collapsed; 
      this.StopAnimation(); 
     } 

     /// <summary> 
     /// Starts the animation 
     /// </summary> 
     public void StartAnimation() 
     { 
      if (!_isInitialized) 
       this.Initialize(); 

      BeginAnimation(FrameIndexProperty, _animation); 
     } 

     /// <summary> 
     /// Stops the animation 
     /// </summary> 
     public void StopAnimation() 
     { 
      BeginAnimation(FrameIndexProperty, null); 
     } 

     #endregion Public Instance Methods 
    } 

लेकिन मैं त्रुटि प्राप्त करें:

The URI prefix is not recognized.

मुझे यकीन नहीं है कि मुझे त्रुटि क्यों मिलती है। क्या कोई कृपया मेरी यह मदद कर सकता है ?

उत्तर

23

WPF में एक एनिमेटेड .gif दिखाने के लिए एक बहुत ही आसान तरीका है - का उपयोग MediaElement

उदाहरण:

<MediaElement x:Name="myGif" MediaEnded="myGif_MediaEnded" UnloadedBehavior="Manual"  Source="file://C:\waiting.GIF" LoadedBehavior="Play" Stretch="None"/> 

आप अंतहीन पाश के लिए .gif चाहते हैं, लेकिन अगर यह केवल एक सीमित निर्दिष्ट करता है .gif फ़ाइल में दोहराता की राशि, आप MediaEnded हुक कर सकते हैं और सिर्फ एनीमेशन (UnloadedBehaviorManual करने के लिए सेट करने के लिए सुनिश्चित करें) को पुनः आरंभ:

private void myGif_MediaEnded(object sender, RoutedEventArgs e) 
    { 
     myGif.Position = new TimeSpan(0, 0, 1); 
     myGif.Play(); 
    } 
+1

धन्यवाद, लेकिन यह मेरा प्रश्न समाधान नहीं है। – UFO

+0

मैं जीआईएफ स्क्रीन पर कहां खेलता हूं, इस स्थान को कैसे निर्दिष्ट करूं? – zetar

+2

कुछ घन – Wobbles

0

इस प्रयास करें: BTW मैं WPF में gif खेलने के लिए कुछ gif छवियों ख़राब हो सकता है इस तरह का उपयोग कर पाया

<controls:GifImage GifSource="/Images/my.gif" Stretch="None" /> 

, और मुझे आश्चर्य है कि क्यों ...

+0

आपको वह नियंत्रण कहां मिल रहा है? –

+0

यह समाधान यहां पाया जा सकता है: https://stackoverflow.com/a/1134340/2772330 –

0

मैं इस के लिए ऋण नहीं ले जा सकते हैं लेकिन यहां केवल एक्सएएमएल में ऐसा करने का एक तरीका है। मैंने प्रोसेसिंग के दौरान स्पिनर को दिखाने/छिपाने के लिए मेरे व्यू मॉडेल में "आईसबुसी" संपत्ति जोड़ा।

 <Image Name="Spinner" Source="Resources/spinner.gif" RenderTransformOrigin="0.5, 0.5"> 
      <Image.Triggers> 
       <EventTrigger RoutedEvent="FrameworkElement.Loaded"> 
        <EventTrigger.Actions> 
         <BeginStoryboard> 
          <Storyboard Storyboard.TargetName="Spinner" Storyboard.TargetProperty="RenderTransform.(RotateTransform.Angle)"> 
           <DoubleAnimation From="0" To="360" BeginTime="0:0:0" Duration="0:0:2" RepeatBehavior="Forever" /> 
          </Storyboard> 
         </BeginStoryboard> 
        </EventTrigger.Actions> 
       </EventTrigger> 
      </Image.Triggers> 
      <Image.RenderTransform> 
       <RotateTransform Angle="0" /> 
      </Image.RenderTransform> 
      <Image.Style> 
       <Style TargetType="Image"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding IsBusy}" Value="False"> 
          <Setter Property="Visibility" Value="Hidden"/> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </Image.Style> 
     </Image> 

लेखक के समाधान में link है।

+0

यह सिर्फ छवि को घूर्णन कर रहा है (अगर स्रोत है तो पहले छवि को चित्रित करें .gif) –