服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - 详解WPF如何在基础控件上显示Loading等待动画

详解WPF如何在基础控件上显示Loading等待动画

2023-04-14 16:28WPF开发者 C#

这篇文章主要为大家详细介绍了WPF如何在基础控件上显示Loading等待动画的效果,文中的示例代码讲解详细,具有一定的学习价值,需要的可以参考一下

WPF 如何在基础控件上显示 Loading 等待动画

  • 框架使用.NET4 至 .NET6
  • Visual Studio 2022;
  • 使用方式需引入命名空间后设置控件的附加属性 wd:Loading.IsShow="true",即可显示默认等待动画效果如下:

详解WPF如何在基础控件上显示Loading等待动画

  • 如需自定义 Loading 一定要 先设置 wd:Loading.Child 在设置 IsShow="true" 。
  • 显示不同 Loading 内容需 wd:Loading.Child ={x:Static wd:NormalLoading.Default} 进行复赋值显示 NormalLoading 效果如下:

详解WPF如何在基础控件上显示Loading等待动画

实现代码

1)创建 BasicControlsExample.xaml 代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<UserControl x:Class="WPFDevelopers.Samples.ExampleViews.BasicControlsExample"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"
             xmlns:wpfdev="https://github.com/WPFDevelopersOrg/WPFDevelopers"
             xmlns:controls="clr-namespace:WPFDevelopers.Samples.Controls"
             mc:Ignorable="d"  Background="{DynamicResource BackgroundSolidColorBrush}"
             d:DesignHeight="450" d:DesignWidth="800" Name="MyBasicControls">
<TextBlock Text="Loading" FontSize="20" Margin="0,20,0,0"/>
                    <WrapPanel Margin="0,10">
                        <Button Content="Loading" Click="Loading_Click"
                            Style="{DynamicResource PrimaryButton}"/>
                        <Button Name="btnLoadingTask" Content="LoadingTask" Click="LoadingTask_Click"
                                Style="{DynamicResource SuccessPrimaryButton}" Margin="10,0"/>
                        <Button Name="btnLoading" Click="BtnLoading_Click" Content="AddLoading"
                                wpfdev:Loading.Child="{x:Static wpfdev:NormalLoading.Default}"
                                Style="{DynamicResource WarningPrimaryButton}"/>
                        <Button Name="btnOffTask" Click="BtnOffTask_Click"
                                Margin="10,0" Content="Off Task"
                                Style="{DynamicResource DangerPrimaryButton}"/>
                    </WrapPanel>
  </UserControl>

2)逻辑 BasicControlsExample.xaml.cs 代码如下:

对控件进行等待动画。

?
1
2
3
4
5
6
7
8
9
10
11
  private void Loading_Click(object sender, RoutedEventArgs e)
      {
          var task = new Task(() => { Thread.Sleep(5000); });
          task.ContinueWith(previousTask => 
          {
              Loading.SetIsShow(MyBasicControls, false);
          }, 
          TaskScheduler.FromCurrentSynchronizationContext());
          Loading.SetIsShow(MyBasicControls, true);
          task.Start();
      }

详解WPF如何在基础控件上显示Loading等待动画

基础控件上添加等待动画。

?
1
2
3
4
5
6
7
private void BtnLoading_Click(object sender, RoutedEventArgs e)
        {
            var task = new Task(() => { Thread.Sleep(5000); });
            task.ContinueWith(previousTask => { Loading.SetIsShow(btnLoading, false); }, TaskScheduler.FromCurrentSynchronizationContext());
            Loading.SetIsShow(btnLoading, true);
            task.Start();
        }

详解WPF如何在基础控件上显示Loading等待动画

关闭基础控件的等待动画。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 private void BtnOffTask_Click(object sender, RoutedEventArgs e)
        {
            if (tokenSource == nullreturn;
            tokenSource.Cancel();
            Loading.SetIsShow(btnLoadingTask, false);
        }
        private CancellationTokenSource tokenSource;
 
        private void LoadingTask_Click(object sender, RoutedEventArgs e)
        {
            tokenSource = new CancellationTokenSource();
            var cancellationToken = tokenSource.Token;
 
            var task = new Task(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    //这里做自己的事情
                    if (tokenSource.IsCancellationRequested)
                        return;
                    Thread.Sleep(1000);
                }
            }, cancellationToken);
            task.ContinueWith(previousTask =>
            {
                if (tokenSource.IsCancellationRequested)
                    return;
                Loading.SetIsShow(btnLoadingTask, false);
            }, TaskScheduler.FromCurrentSynchronizationContext());
            Loading.SetIsShow(btnLoadingTask, true);
            task.Start();
        }

效果图

详解WPF如何在基础控件上显示Loading等待动画

详解WPF如何在基础控件上显示Loading等待动画

到此这篇关于详解WPF如何在基础控件上显示Loading等待动画的文章就介绍到这了,更多相关WPF基础控件显示Loading等待动画内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://mp.weixin.qq.com/s/n9zStOH25f0jh6rfUxqnvA

延伸 · 阅读

精彩推荐
  • C#C#中IDispose接口的实现及为何这么实现详解

    C#中IDispose接口的实现及为何这么实现详解

    这篇文章主要给大家介绍了关于C#中IDispose接口的实现及为何这么实现的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参...

    张Da夫6562022-02-23
  • C#Unity Shader实现玻璃材质效果

    Unity Shader实现玻璃材质效果

    这篇文章主要为大家详细介绍了Unity Shader实现玻璃材质效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    ZzEeRO9062022-09-05
  • C#C# WebApi CORS跨域问题解决方案

    C# WebApi CORS跨域问题解决方案

    本篇文章主要介绍了C# WebApi CORS跨域问题解决方案,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    懒得安分9022022-02-25
  • C#Unity3D使用Shader实现腐蚀消失

    Unity3D使用Shader实现腐蚀消失

    这篇文章主要为大家详细介绍了Unity3D使用Shader实现腐蚀消失,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    星空不语5142022-07-08
  • C#C#简单读写txt文件的方法

    C#简单读写txt文件的方法

    这篇文章主要介绍了C#简单读写txt文件的方法,涉及C#针对文件的基本打开、写入、保存与读取等操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    smartsmile20124292021-11-26
  • C#C# 是 TypeScript 的最佳替补?

    C# 是 TypeScript 的最佳替补?

    TypeScript非常优秀。它完美地结合了强类型和快速开发,因此非常好用,我在许多情况下都会默认选择这个库。但是,世上没有完美的语言,有些情况下Ty...

    晃來晃呿11032021-12-29
  • C#C#-WinForm跨线程修改UI界面的示例

    C#-WinForm跨线程修改UI界面的示例

    这篇文章主要介绍了C#-WinForm跨线程修改UI界面的示例,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...

    UP技术控11642022-10-26
  • C#C#实现六大设计原则之依赖倒置原则

    C#实现六大设计原则之依赖倒置原则

    这篇文章介绍了C#实现六大设计原则之依赖倒置原则的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋...

    痕迹g4002022-12-25