wpf - 如何在 WPF XAML 中制作加载图形?

标签 wpf xaml listbox

所以我有一个小的 WPF XAML,它可以获取我的 RSS 提要的标题并将它们放在一个 ListBox 中。

但是,加载大约需要 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>

最佳答案

我的解决方案是在我的数据和 WPF 之间实现一个异步层。这将 WPF 与数据端的任何延迟完全分离,并为我提供了很好的事件和属性来触发和绑定(bind)。

我在 View Model or Presenter Model architecture 之上构建了它.我写过a blog post about the basics还有一篇关于 my approach to asynchronous View Models 的较长文章.

这是微调器的 XAML。在 DataContext它需要执行加载的 View 模型。取决于其State , LoadingIndicator将使自己可见并再次崩溃。

<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 下获得许可.]

关于wpf - 如何在 WPF XAML 中制作加载图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/473568/

相关文章:

xaml - Generic.xaml xaml 编译在控件样式上给出错误 WMC0610(XBF 生成错误代码 0x3e9)

.net - WP7 防止列表框滚动

c# - 使用异步等待在 MinWindow 上设置 DataContext

c# - 用另一个 ObservableCollection 替换整个 ObservableCollection

wpf - 在 WPF 的 XAML 中将系统颜色前景分配给 TextBlock

c# - 使用 XamlWriter 保存数组

xaml - 项目模板中的 ListView 视觉状态管理器(WinRT、Metro、XAML)

WPF:列表框和虚拟化

c# - 具有更改哈希码的 WPF 列表框和项目

c# - 在 WinForms 或 WPF 中构建 3d 曲线和绘图的控件