zoukankan      html  css  js  c++  java
  • WPF 加载动画

    效果如图:

    关键代码

    实现动画

    <Grid x:Name="LayoutRoot" Background="Transparent"
            ToolTip="Searching...."
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
        <Canvas RenderTransformOrigin="0.5,0.5"
                HorizontalAlignment="Center"
                VerticalAlignment="Center" Width="120"
                Height="120" Loaded="HandleLoaded"
                Unloaded="HandleUnloaded"  >
            <Ellipse x:Name="C0" Width="20" Height="20"
                        Canvas.Left="0"
                        Canvas.Top="0" Stretch="Fill"
                        Fill="#bbc3de" Opacity="1.0"/>
            <Ellipse x:Name="C1" Width="20" Height="20"
                        Canvas.Left="0"
                        Canvas.Top="0" Stretch="Fill"
                        Fill="#bbc3de" Opacity="0.9"/>
            <Ellipse x:Name="C2" Width="20" Height="20"
                        Canvas.Left="0"
                        Canvas.Top="0" Stretch="Fill"
                        Fill="#bbc3de" Opacity="0.8"/>
            <Ellipse x:Name="C3" Width="20" Height="20"
                        Canvas.Left="0"
                        Canvas.Top="0" Stretch="Fill"
                        Fill="#bbc3de" Opacity="0.7"/>
            <Ellipse x:Name="C4" Width="20" Height="20"
                        Canvas.Left="0"
                        Canvas.Top="0" Stretch="Fill"
                        Fill="#bbc3de" Opacity="0.6"/>
            <Ellipse x:Name="C5" Width="20" Height="20"
                        Canvas.Left="0"
                        Canvas.Top="0" Stretch="Fill"
                        Fill="#bbc3de" Opacity="0.5"/>
            <Ellipse x:Name="C6" Width="20" Height="20"
                        Canvas.Left="0"
                        Canvas.Top="0" Stretch="Fill"
                        Fill="#bbc3de" Opacity="0.4"/>
            <Ellipse x:Name="C7" Width="20" Height="20"
                        Canvas.Left="0"
                        Canvas.Top="0" Stretch="Fill"
                        Fill="#bbc3de" Opacity="0.3"/>
            <Ellipse x:Name="C8" Width="20" Height="20"
                        Canvas.Left="0"
                        Canvas.Top="0" Stretch="Fill"
                        Fill="#bbc3de" Opacity="0.2"/>
            <Canvas.RenderTransform>
                <RotateTransform x:Name="SpinnerRotate"
                        Angle="0" />
            </Canvas.RenderTransform>
        </Canvas>
    </Grid>
    
    #region Data
    private readonly DispatcherTimer animationTimer;
    #endregion
    public LoadingMark()
    {
        InitializeComponent();
        animationTimer = new DispatcherTimer(
            DispatcherPriority.ContextIdle, Dispatcher);
        animationTimer.Interval = new TimeSpan(0, 0, 0, 0, 75);
        Start();
    }
    
    #region Private Methods
    private void Start()
    {
        Mouse.OverrideCursor = Cursors.Wait;
        animationTimer.Tick += HandleAnimationTick;
        animationTimer.Start();
    }
    
    private void Stop()
    {
        animationTimer.Stop();
        Mouse.OverrideCursor = Cursors.Arrow;
        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
    

    实现遮挡效果

    使用Border撑满容器,设置背景为灰色,并设置透明度
    绑定Visibility

    <Border Visibility="{Binding Path=Loading}"
        HorizontalAlignment="Stretch" Background="Gray" Opacity="0.3" IsEnabled="False"
        VerticalAlignment="Stretch">
        <local:LoadingMark Width="200" Height="200"/>
    </Border>
    

    示例代码

    LoadingMarks

    参考资料

    How to add a loading animation and finished check mark to a bound observable collection in MVVM?
    Better WPF Circular Progress Bar

  • 相关阅读:
    IIS部署web Service使用浏览器测试
    VS编译运行时提示:应用程序并行配置不正确,无法启动程序
    VS编译失败但是错误输出页中没有显示任何错误信息
    在客户端机器上使用PlSql,登录dba账号提示ORA-01031:insufficient privileges或 ORA-01017: invalid username/password; logon denied错误。
    FTP搭建注意事项
    .Net DLL类库引用时没有注释信息
    Oracle数据库连接超时
    Oracle触发器编译错误及解决方案
    VMware命令行模式安装CentOS 7后设置固定IP
    洲-国家 二级联动整体解决方案
  • 原文地址:https://www.cnblogs.com/Lulus/p/13478664.html
Copyright © 2011-2022 走看看