前台:
1 <UserControl x:Class="PlotTool.LoadingWait" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 IsVisibleChanged="HandleVisibleChanged"> 6 <UserControl.Background> 7 <SolidColorBrush Color="Black" Opacity="0.2" /> 8 </UserControl.Background> 9 <UserControl.Resources> 10 <SolidColorBrush Color="#FF007BE5" x:Key="CirclesColor" /> 11 <!--<SolidColorBrush Color="Black" x:Key="BackgroundColor" Opacity=".20" />--> 12 </UserControl.Resources> 13 14 15 16 <Viewbox Width="100" Height="100" 17 HorizontalAlignment="Center" 18 VerticalAlignment="Center"> 19 <Grid x:Name="LayoutRoot" 20 Background="Transparent" 21 ToolTip="Please wait...." 22 HorizontalAlignment="Center" 23 VerticalAlignment="Center"> 24 <TextBlock Text="加载中..." HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="14" Foreground="#FFE3953D" FontWeight="Bold" /> 25 <Canvas RenderTransformOrigin="0.5,0.5" 26 HorizontalAlignment="Center" 27 VerticalAlignment="Center" Width="120" 28 Height="120" Loaded="HandleLoaded" 29 Unloaded="HandleUnloaded" > 30 <Ellipse x:Name="C0" Width="20" Height="20" 31 Canvas.Left="0" 32 Canvas.Top="0" Stretch="Fill" 33 Fill="{StaticResource CirclesColor}" Opacity="1.0"/> 34 <Ellipse x:Name="C1" Width="20" Height="20" 35 Canvas.Left="0" 36 Canvas.Top="0" Stretch="Fill" 37 Fill="{StaticResource CirclesColor}" Opacity="0.9"/> 38 <Ellipse x:Name="C2" Width="20" Height="20" 39 Canvas.Left="0" 40 Canvas.Top="0" Stretch="Fill" 41 Fill="{StaticResource CirclesColor}" Opacity="0.8"/> 42 <Ellipse x:Name="C3" Width="20" Height="20" 43 Canvas.Left="0" 44 Canvas.Top="0" Stretch="Fill" 45 Fill="{StaticResource CirclesColor}" Opacity="0.7"/> 46 <Ellipse x:Name="C4" Width="20" Height="20" 47 Canvas.Left="0" 48 Canvas.Top="0" Stretch="Fill" 49 Fill="{StaticResource CirclesColor}" Opacity="0.6"/> 50 <Ellipse x:Name="C5" Width="20" Height="20" 51 Canvas.Left="0" 52 Canvas.Top="0" Stretch="Fill" 53 Fill="{StaticResource CirclesColor}" Opacity="0.5"/> 54 <Ellipse x:Name="C6" Width="20" Height="20" 55 Canvas.Left="0" 56 Canvas.Top="0" Stretch="Fill" 57 Fill="{StaticResource CirclesColor}" Opacity="0.4"/> 58 <Ellipse x:Name="C7" Width="20" Height="20" 59 Canvas.Left="0" 60 Canvas.Top="0" Stretch="Fill" 61 Fill="{StaticResource CirclesColor}" Opacity="0.3"/> 62 <Ellipse x:Name="C8" Width="20" Height="20" 63 Canvas.Left="0" 64 Canvas.Top="0" Stretch="Fill" 65 Fill="{StaticResource CirclesColor}" Opacity="0.2"/> 66 <Canvas.RenderTransform> 67 <RotateTransform x:Name="SpinnerRotate" 68 Angle="0" /> 69 </Canvas.RenderTransform> 70 </Canvas> 71 </Grid> 72 </Viewbox> 73 </UserControl>
后台:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace PlotTool { /// <summary> /// Interaction logic for LoadingWait.xaml /// </summary> public partial class LoadingWait : UserControl { #region Data private readonly DispatcherTimer animationTimer; #endregion #region Constructor public LoadingWait() { InitializeComponent(); animationTimer = new DispatcherTimer( DispatcherPriority.ContextIdle, Dispatcher); animationTimer.Interval = new TimeSpan(0, 0, 0, 0, 90); } #endregion #region Private Methods private void Start() { animationTimer.Tick += HandleAnimationTick; animationTimer.Start(); } private void Stop() { animationTimer.Stop(); animationTimer.Tick -= HandleAnimationTick; } private void HandleAnimationTick(object sender, EventArgs e) { SpinnerRotate.Angle = (SpinnerRotate.Angle + 36) % 360; } private void HandleLoaded(object sender, RoutedEventArgs e) { const double offset = Math.PI; const double step = Math.PI * 2 / 10.0; SetPosition(C0, offset, 0.0, step); SetPosition(C1, offset, 1.0, step); SetPosition(C2, offset, 2.0, step); SetPosition(C3, offset, 3.0, step); SetPosition(C4, offset, 4.0, step); SetPosition(C5, offset, 5.0, step); SetPosition(C6, offset, 6.0, step); SetPosition(C7, offset, 7.0, step); SetPosition(C8, offset, 8.0, step); } private void SetPosition(Ellipse ellipse, double offset, double posOffSet, double step) { ellipse.SetValue(Canvas.LeftProperty, 50.0 + Math.Sin(offset + posOffSet * step) * 50.0); ellipse.SetValue(Canvas.TopProperty, 50 + Math.Cos(offset + posOffSet * step) * 50.0); } private void HandleUnloaded(object sender, RoutedEventArgs e) { Stop(); } private void HandleVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { bool isVisible = (bool)e.NewValue; if (isVisible) Start(); else Stop(); } #endregion } }
应用:
1 <Grid x:Name="gd_top_show" Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" Grid.ColumnSpan="3" > 2 <local:LoadingWait x:Name="_loading" /> 3 </Grid> 4 5 xmlns:local="clr-namespace:空间l"