2011-09-05 88 views
1

我想知道什么是下载图像时显示加载动画的最佳方式。加载动画图像

我有一个从互联网URL动态加载的图像。所以在看到我的形象之前我有大约1/2秒的时间,我认为这取决于连接。所以我想在图像仍在下载时为用户显示一个动画。

坦克您的建议

回答

1

我终于通过使用VM(MVVM)解决了我的问题。我解释一下,如果有人有同样的麻烦:

首先,我有我的视图中的列表框连接到我的ViewModel中的列表。我所做的是一个命令连接到的SelectionChanged像这样的事件:

<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> 

然后在我的函数加载盖,我以建盖的url和我创建的BitmapImage。但在此之前,我初始化一个布尔说这是加载:

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

最后,当图像下载我把卸载指示为false。 而在我的视图中,我的控件仅在IsLoadingCover为true时显示(Bool to Visibility Converter):

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

你可以添加一个WindowsFormHost举行GIF动画和显示/隐藏主机是必要的。

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> 

在OnWindowLoaded方法设置的动画形象在WinForm主机控制

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

如果你想在执行其他工作,如下载图像改变你的图形用户界面,你应该异步下载图像。

有几种方法可以做到这一点,例如与BackgroundWorker类(在一个单独的线程上)或者根据你用来下载图像的内容,在同一个线程上异步(这可能是一个更好的主意)。