詳解WPF如何在基礎控件上顯示Loading等待動畫
WPF 如何在基礎控件上顯示 Loading 等待動畫
- 框架使用
.NET4 至 .NET6
; Visual Studio 2022
;- 使用方式需引入命名空間后設置控件的附加屬性
wd:Loading.IsShow="true"
,即可顯示默認等待動畫效果如下:
- 如需自定義
Loading
一定要 先設置wd:Loading.Child
在設置IsShow="true"
。 - 顯示不同
Loading
內容需wd:Loading.Child ={x:Static wd:NormalLoading.Default}
進行復賦值顯示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(); ??????}
基礎控件上添加等待動畫。
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(); ????????}
關閉基礎控件的等待動畫。
?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(); ????????}
效果圖
到此這篇關于詳解WPF如何在基礎控件上顯示Loading等待動畫的文章就介紹到這了,更多相關WPF基礎控件顯示Loading等待動畫內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C# 數(shù)據(jù)庫鏈接字符串加密解密工具代碼詳解
本文通過代碼給大家介紹C# 數(shù)據(jù)庫鏈接字符串加密解密工具的相關知識,實現(xiàn)思路大概是使用兩個數(shù)對連接字符串進行加密,再用這兩個數(shù)進行解密,具體詳細代碼,大家參考下本文2018-05-05C# WinForm調用Shell_NotifyIcon的示例代碼
這篇文章主要介紹了C# WinForm調用Shell_NotifyIcon的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-11-11C#實現(xiàn)Oracle批量寫入數(shù)據(jù)的方法詳解
往數(shù)據(jù)庫批量寫入數(shù)據(jù),這個功能使用頻率相對還是比較高的,特別是在做一些導入等功能的時候。本文為大家介紹了C#實現(xiàn)Oracle批量寫入數(shù)據(jù)的方法,需要的可以參考一下2022-11-11