2010-07-27 11 views
1

Lorsque j'ai développé ma WPF application PC, j'ai utilisé le code suivant pour afficher mon image gif, comme je l'ai réalisé qu'il n'y a pas moyen facile de le faire: -affichage image gif

XAML:

  <StackPanel Height="25" Name="stkProgressBar"> 
       <wfi:WindowsFormsHost Background="Transparent" HorizontalAlignment="Center"> 
        <winForms:PictureBox x:Name="pictureBoxLoading" Visible="False"> 
        </winForms:PictureBox> 
       </wfi:WindowsFormsHost> 

      </StackPanel> 

XAML.cs:

private void Page_Loaded(object sender, RoutedEventArgs e) 
    { 
     this.pictureBoxLoading.Image = Properties.Resources.progressbar; 
    } 

maintenant, quand j'utilise la même approche pour afficher l'image dans mon application WPF Browser (XBAP), il ne se charge pas la page en raison de ce code XAML? Des idées pourquoi est-ce le cas et s'il y a un autre moyen d'afficher des GIF sur une page XBAP?

Merci, Abhi.

==================

Mis à jour à 11 27/07/2010: AM

Maintenant, j'ai essayé le code suivant: -

XAML:

<Image Name="Image1"></Image> 

XAML.cs:

 Stream imageStreamSource = new FileStream(@"C:\Inetpub\ExchangeRate\ExchangeRate\Image\progressbar.gif", FileMode.Open, FileAccess.Read, FileShare.Read); 
     GifBitmapDecoder decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); 

     Int32Animation anim = new Int32Animation(0, decoder.Frames.Count - 1, new Duration(new TimeSpan(0, 0, 0, decoder.Frames.Count/10, (int)((decoder.Frames.Count/10.0 - decoder.Frames.Count/10) * 1000)))); 
     anim.RepeatBehavior = RepeatBehavior.Forever; 

     BitmapSource bitmapSource = decoder.Frames[0]; 

     Image1.Source = bitmapSource; 
     Image1.Stretch = Stretch.None; 

L'image est correcte, mais elle n'est pas animée. J'apprécierais toute aide à cet égard.

Abhi.

+0

Encore une information. La page ne donne pas d'erreur de compilation, mais lors de l'exécution de la page, il erreurs. –

+0

http://efreedom.com/Question/1-210922/How-do-I-get-an-animated-gif-to-work-in-WPF – Ragunathan

Répondre

0

Après avoir travaillé pendant un jour, je l'ai élaboré en utilisant une approche différente. Au lieu d'utiliser une image gif, j'ai créé un contrôle utilisateur comme suit: -

<UserControl.Resources> 
    <Color x:Key="FilledColor" A="255" B="112" R="147" G="160"/> 
    <Color x:Key="UnfilledColor" A="0" B="112" R="147" G="160"/> 

    <Storyboard x:Key="Animation0" FillBehavior="Stop" BeginTime="00:00:00.0" RepeatBehavior="Forever"> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetName="_00" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> 
      <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/> 
      <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/> 
     </ColorAnimationUsingKeyFrames> 
    </Storyboard> 

    <Storyboard x:Key="Animation1" BeginTime="00:00:00.2" RepeatBehavior="Forever"> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetName="_01" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> 
      <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/> 
      <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/> 
     </ColorAnimationUsingKeyFrames> 
    </Storyboard> 

    <Storyboard x:Key="Animation2" BeginTime="00:00:00.4" RepeatBehavior="Forever"> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetName="_02" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> 
      <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/> 
      <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/> 
     </ColorAnimationUsingKeyFrames> 
    </Storyboard> 

    <Storyboard x:Key="Animation3" BeginTime="00:00:00.6" RepeatBehavior="Forever"> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetName="_03" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> 
      <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/> 
      <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/> 
     </ColorAnimationUsingKeyFrames> 
    </Storyboard> 

    <Storyboard x:Key="Animation4" BeginTime="00:00:00.8" RepeatBehavior="Forever"> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetName="_04" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> 
      <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/> 
      <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/> 
     </ColorAnimationUsingKeyFrames> 
    </Storyboard> 

    <Storyboard x:Key="Animation5" BeginTime="00:00:01.0" RepeatBehavior="Forever"> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetName="_05" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> 
      <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/> 
      <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/> 
     </ColorAnimationUsingKeyFrames> 
    </Storyboard> 

    <Storyboard x:Key="Animation6" BeginTime="00:00:01.2" RepeatBehavior="Forever"> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetName="_06" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> 
      <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/> 
      <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/> 
     </ColorAnimationUsingKeyFrames> 
    </Storyboard> 

    <Storyboard x:Key="Animation7" BeginTime="00:00:01.4" RepeatBehavior="Forever"> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetName="_07" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> 
      <SplineColorKeyFrame KeyTime="00:00:00.0" Value="{StaticResource FilledColor}"/> 
      <SplineColorKeyFrame KeyTime="00:00:01.6" Value="{StaticResource UnfilledColor}"/> 
     </ColorAnimationUsingKeyFrames> 
    </Storyboard> 
</UserControl.Resources> 

<UserControl.Triggers> 
    <EventTrigger RoutedEvent="FrameworkElement.Loaded"> 
     <BeginStoryboard Storyboard="{StaticResource Animation0}"/> 
     <BeginStoryboard Storyboard="{StaticResource Animation1}"/> 
     <BeginStoryboard Storyboard="{StaticResource Animation2}"/> 
     <BeginStoryboard Storyboard="{StaticResource Animation3}"/> 
     <BeginStoryboard Storyboard="{StaticResource Animation4}"/> 
     <BeginStoryboard Storyboard="{StaticResource Animation5}"/> 
     <BeginStoryboard Storyboard="{StaticResource Animation6}"/> 
     <BeginStoryboard Storyboard="{StaticResource Animation7}"/> 
    </EventTrigger> 
</UserControl.Triggers> 

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 
     <ColumnDefinition Width="Auto" MinWidth="150" /> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
    <Canvas Grid.Row="1" Grid.Column="1" Height="20" HorizontalAlignment="Left" VerticalAlignment="Top"> 
     <Canvas.Resources> 
      <Style TargetType="Ellipse"> 
       <Setter Property="Width" Value="15"/> 
       <Setter Property="Height" Value="15" /> 
       <Setter Property="Fill" Value="#FFFFFFFF" /> 
      </Style> 

      <Style TargetType="Rectangle"> 
       <Setter Property="Width" Value="12"/> 
       <Setter Property="Height" Value="13" /> 
       <Setter Property="Fill" Value="#FFFFFFFF" /> 
       <Setter Property="RadiusX" Value="2" /> 
       <Setter Property="RadiusY" Value="2" /> 
      </Style> 

     </Canvas.Resources> 

     <Rectangle x:Name="_00" Canvas.Top="6" Canvas.Left="15"></Rectangle> 
     <Rectangle x:Name="_01" Canvas.Top="6" Canvas.Left="30"></Rectangle> 
     <Rectangle x:Name="_02" Canvas.Top="6" Canvas.Left="45"></Rectangle> 
     <Rectangle x:Name="_03" Canvas.Top="6" Canvas.Left="60"></Rectangle> 
     <Rectangle x:Name="_04" Canvas.Top="6" Canvas.Left="75"></Rectangle> 
     <Rectangle x:Name="_05" Canvas.Top="6" Canvas.Left="90"></Rectangle> 
     <Rectangle x:Name="_06" Canvas.Top="6" Canvas.Left="105"></Rectangle> 
     <Rectangle x:Name="_07" Canvas.Top="6" Canvas.Left="120"></Rectangle> 
    </Canvas> 
<!-- <TextBlock Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Foreground="#AAA" TextAlignment="Center" FontSize="15" Text="{Binding Source={StaticResource Model}, Path=StatusMessage}"/> --> 
</Grid> 

Dans la classe UserControl, j'ai créé la méthode comme suit: -

public void Show() 
    { 
     this.Visibility = Visibility.Visible; 
    } 
    public void Hide() 
    { 
     this.Visibility = Visibility.Hidden; 
    } 

This link m'a orienté dans cette direction.