詳解WPF如何在基礎(chǔ)控件上顯示Loading等待動畫
WPF 如何在基礎(chǔ)控件上顯示 Loading 等待動畫
- 框架使用
.NET4 至 .NET6; Visual Studio 2022;- 使用方式需引入命名空間后設(shè)置控件的附加屬性
wd:Loading.IsShow="true",即可顯示默認等待動畫效果如下:

- 如需自定義
Loading一定要 先設(shè)置wd:Loading.Child在設(shè)置IsShow="true"。 - 顯示不同
Loading內(nèi)容需wd:Loading.Child ={x:Static wd:NormalLoading.Default}進行復(fù)賦值顯示NormalLoading效果如下:

實現(xiàn)代碼
1)創(chuàng)建 BasicControlsExample.xaml 代碼如下:
<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 代碼如下:
對控件進行等待動畫。
??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();
??????}

基礎(chǔ)控件上添加等待動畫。
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();
????????}

關(guān)閉基礎(chǔ)控件的等待動畫。
?private?void?BtnOffTask_Click(object?sender,?RoutedEventArgs?e)
????????{
????????????if?(tokenSource?==?null)?return;
????????????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();
????????}
效果圖


到此這篇關(guān)于詳解WPF如何在基礎(chǔ)控件上顯示Loading等待動畫的文章就介紹到這了,更多相關(guān)WPF基礎(chǔ)控件顯示Loading等待動畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity存儲游戲數(shù)據(jù)的多種方法小結(jié)
這篇文章主要介紹了Unity存儲游戲數(shù)據(jù)的幾種方法,在游戲開發(fā)中,存儲游戲數(shù)據(jù)是非常重要的,因為游戲數(shù)據(jù)決定了游戲的各個方面,例如游戲的進度、玩家的成就、游戲的設(shè)置,需要的朋友可以參考下2023-02-02
C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘二
上文對數(shù)據(jù)結(jié)構(gòu)與算法,有了一個簡單的概述與介紹,這篇文章,我們介紹一中典型數(shù)據(jù)結(jié)構(gòu)——線性結(jié)構(gòu)2012-10-10
C#中csv文件與DataTable互相導(dǎo)入處理實例解析
這篇文章主要介紹了C#中csv文件與DataTable互相導(dǎo)入處理實例解析,非常實用的功能,需要的朋友可以參考下2014-08-08
C#緩存之SqlCacheDependency用法實例總結(jié)
這篇文章主要介紹了C#緩存之SqlCacheDependency用法,在C#程序設(shè)計中有一定的實用價值,需要的朋友可以參考下2014-08-08

