【问题标题】:Loading Animation Images加载动画图像
【发布时间】:2011-11-10 15:28:05
【问题描述】:

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

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

感谢您的建议

【问题讨论】:

    标签: c# wpf image xaml animation


    【解决方案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 为真时可见(布尔到可见性转换器):

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

    【讨论】:

      【解决方案2】:

      您可以添加一个 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;
      }
      

      【讨论】:

        【解决方案3】:

        如果您想在执行其他工作时更改您的 GUI,例如下载图像,您应该异步下载图像。

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

        【讨论】:

          猜你喜欢
          • 2016-03-31
          • 2016-05-24
          • 1970-01-01
          • 2012-08-20
          • 2019-03-24
          • 2021-07-25
          • 1970-01-01
          • 2012-07-16
          • 1970-01-01
          相关资源
          最近更新 更多