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

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

實(shí)現(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 代碼如下:
對(duì)控件進(jìn)行等待動(dòng)畫。
??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ǔ)控件上添加等待動(dòng)畫。
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ǔ)控件的等待動(dòng)畫。
?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等待動(dòng)畫的文章就介紹到這了,更多相關(guān)WPF基礎(chǔ)控件顯示Loading等待動(dòng)畫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 數(shù)據(jù)庫鏈接字符串加密解密工具代碼詳解
本文通過代碼給大家介紹C# 數(shù)據(jù)庫鏈接字符串加密解密工具的相關(guān)知識(shí),實(shí)現(xiàn)思路大概是使用兩個(gè)數(shù)對(duì)連接字符串進(jìn)行加密,再用這兩個(gè)數(shù)進(jìn)行解密,具體詳細(xì)代碼,大家參考下本文2018-05-05
C# WinForm調(diào)用Shell_NotifyIcon的示例代碼
這篇文章主要介紹了C# WinForm調(diào)用Shell_NotifyIcon的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-11-11
C#實(shí)現(xiàn)Oracle批量寫入數(shù)據(jù)的方法詳解
往數(shù)據(jù)庫批量寫入數(shù)據(jù),這個(gè)功能使用頻率相對(duì)還是比較高的,特別是在做一些導(dǎo)入等功能的時(shí)候。本文為大家介紹了C#實(shí)現(xiàn)Oracle批量寫入數(shù)據(jù)的方法,需要的可以參考一下2022-11-11
Unity使用DoTween實(shí)現(xiàn)拋物線效果
這篇文章主要為大家詳細(xì)介紹了Unity使用DoTween實(shí)現(xiàn)拋物線效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
c#實(shí)現(xiàn)把漢字轉(zhuǎn)為帶田字格背景的jpg圖片
這篇文章主要介紹了c#實(shí)現(xiàn)把漢字轉(zhuǎn)為帶田字格背景的jpg圖片示例,需要的朋友可以參考下2014-03-03
C#訪問SqlServer設(shè)置鏈接超時(shí)的方法
這篇文章主要介紹了C#訪問SqlServer設(shè)置鏈接超時(shí)的方法,涉及CommandTimeout屬性的相關(guān)設(shè)置技巧,非常簡單實(shí)用,需要的朋友可以參考下2015-06-06

