开发者

Loading Animation Images

开发者 https://www.devze.com 2023-04-03 06:05 出处:网络
I would like to know what is the best way to show a loading animation while downloading an image. I have an image which is loaded dynamically from an internet URL. So I have about 1/2 second before s

I would like to know what is the best way to show a loading animation while downloading an image.

I have an image which is loaded dynamically from an internet URL. So I have about 1/2 second before seeing my image and i think it depends of the connectio开发者_如何学JAVAn. So I would like to show an animation for the user while the image is still downloading.

Tanks for your suggestions


I finally solved my problem by using the VM (MVVM). I explain if someone have the same kind of trouble :

First I have Listbox in my view which is connected to a List in my ViewModel. What I did is connect a command to the event SelectionChanged like this :

<ListBox x:Name="EbookBox" ItemTemplate="{StaticResource ListItemTemplate}" Height="505" ItemsSource="{Binding OBooks, Mode=OneWay}" SelectedItem="{Binding SelectedBook, Mode=TwoWay}">
   <i:Interaction.Triggers>
      <i:EventTrigger EventName="SelectionChanged">
         <cmd:EventToCommand Command="{Binding LoadCoverCommand, Mode=OneWay}" />
      </i:EventTrigger>
   </i:Interaction.Triggers>
</ListBox>

Then in my function Load cover, I construct the url of the cover and I create a bitmapImage. But before, I initialize a boolean to say it's loading :

IsLoadingCover = true;
// Create the Bitmap and save it to the caching directory
CurrentCover = new BitmapImage(new Uri(cover));
CurrentCover.DownloadCompleted += DownloadCompleted;

Finally when the image is download i put the loading indication to false. And in my View, my control is only visible when IsLoadingCover is true (Bool to Visibility Converter) :

<control:LoadingAnimation HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,123,0,0" Visibility="{Binding IsLoadingCover,Converter={StaticResource BoolToVisibilityConverter}}"/>


You could add a WindowsFormHost to hold an animated gif and show/hide the host as necessary.

xmlns:winFormInteg="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:winForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"      
<winFormInteg:WindowsFormsHost
     Width="15"
     Height="15"
     Name="window_lading_anim"
     Loaded="OnWindowLoaded">
     <winForms:PictureBox x:Name="pictureBoxLoading"/>
</winFormInteg:WindowsFormsHost>

In the OnWindowLoaded method set the animated image in the Winform host control

private void OnWindowLoaded(object sender, RoutedEventArgs e)
{
  pictureBoxLoading.Image = Properties.Resources.myAnimatedGif;
}


If you want to change your GUI while performing other work, like downloading an image, you should download the image asynchronously.

There are several ways to do that, e.g. with the BackgroundWorkerclass (on a separate thread) or, depending on what you use to download the image, asynchronously on the same thread (which might be a better idea).

0

精彩评论

暂无评论...
验证码 换一张
取 消