如何在WPF XAML中制作加载图形? [英] How to make a loading graphic in WPF XAML?

查看:69
本文介绍了如何在WPF XAML中制作加载图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个WPF XAML,它可以获取RSS feed的标题并将其放在ListBox中.

So I have a little WPF XAML that gets the titles of my RSS feed and puts them in a ListBox.

但是,加载大约需要2秒钟.

However, it takes about 2 seconds to load.

如何在列表框中放置某种AJAXy旋转图形,直到有数据为止?

How can I have put some kind of AJAXy spinning graphic in the ListBox until the data is there?

这是代码:

<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与数据方面的任何延迟脱钩,并为我提供了触发和绑定的良好事件和属性.

My solution for this is to implement a asynchronous layer between my data and WPF. This totally decouples WPF from any delays on the data side and gives me nice events and properties to trigger and bind against.

我在有关基础知识的博客文章和有关我的异步视图模型方法.

这是微调器的XAML.在DataContext中,它需要执行加载的View Model.根据其StateLoadingIndicator将使其自身可见并再次折叠.

Here is the XAML of the spinner. In the DataContext it needs the View Model that does the loading. Depending on its State, the LoadingIndicator will make itself visible and collapse again.

<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许可的许可.]

[Source Copyright © 2009 dasz.at OG; This work is licensed under a MIT License.]

这篇关于如何在WPF XAML中制作加载图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆