2011-09-05 77 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類(在一個單獨的線程上)或者根據你用來下載圖像的內容,在同一個線程上異步(這可能是一個更好的主意)。