开发者

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

开发者 https://www.devze.com 2023-04-14 10:51 出处:网络 作者: WPF开发者
wpF 如何在基础控件上显示 Loading 等待动画 框架使用.NET4 至 .NET6;Visual Studio 2022;使用方式需引入命名空间后设置控件的附加属性 wd:Loading.IsShow="true",即可显示默认等待动画效果如下:

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 代码如下:

<UserControlx: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="{DynamicResourceBackgroundSolidColorBrush}"
d:DesignHeight="450"d:DesignWidth="800"Name="MyBasicControls">
<TextblockText="Loading"FontSize="20"Margin="0,20,0,0"/>
<WrapPanelMargin="0,10">
<ButtonContent="Loading"Click="Loading_Click"
Style="{DynamicResourcePrimaryButton}"/>
<ButtonName="btnLoadingTask"C编程客栈ontent="LoadingTask"Click="LoadingTask_Click"
Style="{DynamicResourceSuccessPrimaryButton}"Margin="10,0"/>
<ButtonName="btnLoading"Click="BtnLoading_Click"Content="AddLoading"
wpfdev:Loading.Child="{x:Staticwpfdev:NormalLoading.Default}"
Style="{DynamicResourceWarningPrimaryButton}"/>
<ButtonName="btnOffTask"Click="BtnOffTask_Click"
Margin="10,0"Content="OffTask"
Style="{DynamicResourceDangerPrimaryButton}"/>
</WrapPanel>
</UserControl>

2)逻辑 Bas编程客栈icControlsExample.xaml.cs 代码如下:

对控件进行等待动画。

privatevoidLoading_Click(objectsender,RoutedEventArgse)
{
vartask=newTask(()=>{Thread.Sleep(5000);});
task.ContinueWith(previousTask=>
{
Loading开发者_JS培训.SetIsShow(MyBasicControls,false);
},
TaskScheduler.FromCurrentSynchronizationContext());
Loading.SetIsShow(MyBasicControls,true);
taswww.devze.comk.Start();
}

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

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

privatevoidBtnLoading_Click(objectsender,RoutedEventArgse)
{
vartask=newTask(()=>{Thread.Sleep(5000);});
task.ContinueWith(previousTask=>{Loading.SetIsShow(btnLoading,http://www.devze.comfalse);},TaskScheduler.FromCurrentSynchronizationContext());
Loading.SetIsShow(btnLoading,true);
task.Start();
}

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

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

privatevoidBtnOffTask_Click(objectsender,RoutedEventArgse)
{
if(tokenSource==null)return;
tokenSource.Cancel();
Loading.SetIsShow(btnL编程客栈oadingTask,false);
}
privateCancellationTokenSourcetokenSource;

privatevoidLoadingTask_Click(objectsender,RoutedEventArgse)
{
tokenSource=newCancellationTokenSource();
varcancellationToken=tokenSource.Token;

vartask=newTask(()=>
{
for(inti=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等待动画内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号