How to make a loading graphic in WPF XAML?

限于喜欢 提交于 2019-12-05 08:23:45

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.

I built this on top of the View Model or Presenter Model architecture. I've written a blog post about the basics and a longer piece about my approach to asynchronous View Models.

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>

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

You could could create the spinny-loady-thingy and add it to the AdornerLayer of the ListBox.

The following videos are from mix08 and walk you through doing exactly what you want.

Part 1

Part 2

(watch in succession if possible. it is in silverlight but will point you in the right direction.)

Have Fun.

You could exploit the IsAsynchronous property of the XmlDataProvider together with the DataChanged event (I didn't try): have a look at the documentation. ;-)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!