使用 WPF 创建预加载控件

Introduction

At the time when WPF applications do a very long process like getting response from a web server, download file from a distant server, search files, etc., this control can be used to make the wait time more interactive. This is an alternative method to create preloader by using WPF methods instead of using GIF animation images. GIF animation might require more bitmap processing.

This demo will explain how to use this control in your projects. The solution used for this demo is created using Visual Studio 2008 (WPF, C#, .NET 3.5).

Using the Code

This user control is created with four rectangle blocks animated sequentially with Width property with a defined speed.

The following XAML is to create the rectangle block: Block.xaml

Height and width property is set by the PreLoaderControl.xaml control automatically. Developer would not require to change the values of these properties.

<UserControl x:Class="PreLoader.CustomControls.Block"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="Auto" Width="Auto"> <Grid x:Name="PreLoaderBlock"> <Rectangle Width="Auto" Height="Auto" Fill="Red" Margin="0"> <Rectangle.Style> <Style TargetType="{x:Type Rectangle}"> <Setter Property="RenderOptions.BitmapScalingMode" Value="NearestNeighbor" /> <Setter Property="RenderOptions.EdgeMode" Value="Aliased" /> </Style> </Rectangle.Style> </Rectangle> </Grid> </UserControl>

The Fill property can be changed to your desired color or bind with a style theme.

Fill="{DynamicResource PreLoaderColor}"

The resource PreLoaderColor can be defined in the App.xaml or in any style theme.

<ResourceDictionary>
    <SolidColorBrush x:Key="PreLoaderColor" Color="Red" /> </ResourceDictionary>

The following xaml is to define the animation: PreLoaderControl.xaml.

The UserControl.Resources holds the definition for the storyboard animation targeting the Width property for all the four rectangle blocks defined in the Grid as shown below.

The speed of the animation can be adjusted with the property SpeedRatio.

Completed event is created in each storyboard to notify when the animation is completed respectively.

<UserControl x:Class="PreLoader.CustomControls.PreLoaderControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:PreLoader.CustomControls" Height="{Binding}" Width="{Binding}" Loaded="UserControl_Loaded"> <UserControl.Resources> <Storyboard x:Key="ProgressAnimation1" SpeedRatio="12"> <DoubleAnimation Storyboard.TargetName="block1" Storyboard.TargetProperty="Width" From="16" To="0" Completed="ProgressAnimation1_Completed" Duration="0:0:2"/> </Storyboard> <Storyboard x:Key="ProgressAnimation2" SpeedRatio="12"> <DoubleAnimation Storyboard.TargetName="block2" Storyboard.TargetProperty="Width" From="16" To="0" Completed="ProgressAnimation2_Completed" Duration="0:0:2" /> </Storyboard> <Storyboard x:Key="ProgressAnimation3" SpeedRatio="12"> <DoubleAnimation Storyboard.TargetName="block3" Storyboard.TargetProperty="Width" From="16" To="0" Completed="ProgressAnimation3_Completed" Duration="0:0:2" /> </Storyboard> <Storyboard x:Key="ProgressAnimation4" SpeedRatio="12"> <DoubleAnimation Storyboard.TargetName="block4" Storyboard.TargetProperty="Width" From="16" To="0" Completed="ProgressAnimation4_Completed" Duration="0:0:2" /> </Storyboard> </UserControl.Resources> <Grid Width="Auto" Height="Auto" > <Grid HorizontalAlignment="Left" x:Name="gridBlock1" VerticalAlignment="Top" Margin="0,0,0,0" > <local:Block x:Name="block1" RenderTransformOrigin="0.5,4.3689" HorizontalAlignment="Stretch" Height="Auto" Width="Auto" VerticalAlignment="Stretch"> <local:Block.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <TranslateTransform/> </TransformGroup> </local:Block.RenderTransform> </local:Block> <

转载于:https://www.cnblogs.com/mschen/p/4313698.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值