2009-01-23 58 views
5

所以我有一個WPF XAML獲取我的RSS提要的標題,並將它們放在一個ListBox中。如何在WPF XAML中創建加載圖形?

但是,加載需要大約2秒。

如何在ListBox中放置某種AJAXy旋轉圖形,直到數據在那裏?

下面是代碼:

<Window x:Class="WpfApplication5.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
    <StackPanel> 
     <StackPanel.Resources> 
      <XmlDataProvider x:Key="ExternalColors" Source="http://www.tanguay.info/web/rss" XPath="/"/> 
     </StackPanel.Resources> 
     <TextBlock Text="RSS Feeds:"/> 
     <ListBox Name="lbColor" Height="200" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource ExternalColors}, XPath=//item/title}"/> 

     <TextBlock Text="You selected this feed:"/> 
     <TextBox 
      Text="{Binding ElementName=lbColor, Path=SelectedValue}" 
      Background="{Binding ElementName=lbColor, Path=SelectedValue}"> 
     </TextBox> 
    </StackPanel> 
</Window> 

回答

6

我的解決方案是實現我的數據和WPF之間的異步層。這完全分離了WPF在數據方面的任何延遲,併爲我提供了很好的事件和屬性來觸發和綁定。

我在View Model or Presenter Model architecture之上建立了這個。我寫了a blog post about the basics和一個關於my approach to asynchronous View Models的較長一段。

這裏是微調器的XAML。在DataContext它需要執行加載的視圖模型。根據StateLoadingIndicator將自己可見並重新崩潰。

<UserControl x:Class="App.WPF.CustomControls.LoadingIndicator" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Height="20" 
      Width="20"> 
    <UserControl.Style> 
     <Style TargetType="{x:Type UserControl}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding State}" 
          Value="Active"> 
        <Setter Property="Visibility" 
          Value="Collapsed" /> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding State}" 
          Value="Loading"> 
        <Setter Property="Visibility" 
          Value="Visible" /> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding State}" 
          Value="Invalid"> 
        <Setter Property="Background" 
          Value="Red" /> 
        <Setter Property="Visibility" 
          Value="Visible" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </UserControl.Style> 
    <UserControl.Triggers> 
     <EventTrigger RoutedEvent="UserControl.Loaded"> 
      <BeginStoryboard> 
       <Storyboard TargetName="Rotator" 
          TargetProperty="Angle"> 
        <DoubleAnimation By="360" 
            Duration="0:0:2" 
            RepeatBehavior="Forever" /> 
       </Storyboard> 
      </BeginStoryboard> 
     </EventTrigger> 
    </UserControl.Triggers> 
    <Rectangle Stroke="Aqua" 
       StrokeThickness="2" 
       Width="10" 
       Height="10"> 
     <Rectangle.RenderTransform> 
      <RotateTransform x:Name="Rotator" 
          Angle="0" 
          CenterX="5" 
          CenterY="5" /> 
     </Rectangle.RenderTransform> 
    </Rectangle> 
</UserControl> 

[來源版權所有© 2009年dasz.at OG;這項工作是在MIT License的許可。]

+0

你殺了你的博客? – bevacqua 2012-11-10 13:35:10

1

你可以可以創建spinny-loady-啄並將其添加到列表框的AdornerLayer。

1

以下視頻來自mix08,並引導您完全按照自己的意願進行操作。

Part 1

Part 2

(手錶陸續如果可能的話,它是在Silverlight,但將指向您在正確的方向。)

樂趣。

0

您可以利用XmlDataProviderIsAsynchronous屬性和DataChanged事件(我沒有嘗試):查看文檔。 ;-)