Silverlight中的WattingDialog
Silverlight中用经常有耗时操作,此时就要用WattingDialog改善用户体验
主要思想有二个。
1. 用ChildWindow来作WattingDialog的载体。
2. 用一个线程来显示WattingDialog.
WattingDialog.xaml
原来的ChildWindow有标题和边框,并且显示时会有弹出动画。标题、边框和弹出动画都是没用的,应当删除。先用Blend=>edit a copy.把没用的东西删除。
<sdk:ChildWindow xmlns:my= "clr-namespace:Controls.SL" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sdk= "http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class= "Controls.SL.WattingDialog" Width= "350" Height= "80" > <sdk:ChildWindow.Resources> <Style x:Key= "ChildWindowStyle1" TargetType= "sdk:ChildWindow" > <Setter Property= "IsTabStop" Value= "false" /> <Setter Property= "TabNavigation" Value= "Cycle" /> <Setter Property= "HorizontalAlignment" Value= "Center" /> <Setter Property= "VerticalAlignment" Value= "Center" /> <Setter Property= "HorizontalContentAlignment" Value= "Stretch" /> <Setter Property= "VerticalContentAlignment" Value= "Stretch" /> <Setter Property= "OverlayBrush" Value= "#7F000000" /> <Setter Property= "OverlayOpacity" Value= "1" /> <Setter Property= "Template" > <Setter.Value> <ControlTemplate TargetType= "sdk:ChildWindow" > <Grid x:Name= "Root" > <Grid x:Name= "Overlay" Background= "{TemplateBinding OverlayBrush}" HorizontalAlignment= "Stretch" Margin= "0" Opacity= "{TemplateBinding OverlayOpacity}" VerticalAlignment= "Top" /> <Grid x:Name= "ContentRoot" HorizontalAlignment= "{TemplateBinding HorizontalAlignment}" Height= "{TemplateBinding Height}" RenderTransformOrigin= "0.5,0.5" VerticalAlignment= "{TemplateBinding VerticalAlignment}" Width= "{TemplateBinding Width}" > <Border CornerRadius= "2" > <Grid> <Border Background= "{TemplateBinding Background}" > <ContentPresenter x:Name= "ContentPresenter" ContentTemplate= "{TemplateBinding ContentTemplate}" Content= "{TemplateBinding Content}" HorizontalAlignment= "{TemplateBinding HorizontalContentAlignment}" VerticalAlignment= "{TemplateBinding VerticalContentAlignment}" /> </Border> </Grid> </Border> </Grid> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </sdk:ChildWindow.Resources> <sdk:ChildWindow.Style> <StaticResource ResourceKey= "ChildWindowStyle1" /> </sdk:ChildWindow.Style> <Grid x:Name= "LayoutRoot" > <Canvas > <Rectangle RadiusX= "10" RadiusY= "10" Width= "350" Height= "80" > <Rectangle.Fill> <SolidColorBrush Color= "Silver" ></SolidColorBrush> </Rectangle.Fill> </Rectangle> <TextBlock VerticalAlignment= "Center" FontWeight= "Thin" FontSize= "30" Text= "等一下吧。。。" Canvas.Left= "72" Canvas.Top= "20" ></TextBlock> </Canvas> </Grid> </sdk:ChildWindow> |
WattingDialog.xaml.cs
namespace Controls.SL { public partial class WattingDialog : ChildWindow { public WattingDialog() { InitializeComponent(); } public void ShowWattingDialog() { this .Show(); } public void CloseWattingDialog() { this .Close(); } } } |
此外,由于WaittingDalog要在另外一个线程中显示。因此用了BackgroundWorker来建立显示线程。
WattingHelper.cs
public class WattingHelper { private WattingDialog wattingDialog = null ; private Action DoWorkAction = null ; private FrameworkElement DependencyObj = null ; public WattingHelper(FrameworkElement dObj, Action DoWork) { this .DependencyObj = dObj; this .DoWorkAction = DoWork; BackgroundWorker bw= new BackgroundWorker (); bw.DoWork += new DoWorkEventHandler(bw_DoWork); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); bw.RunWorkerAsync(); } void bw_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e) { DependencyObj.Dispatcher.BeginInvoke( new Action(() => { wattingDialog.CloseWattingDialog(); wattingDialog = null ; })); } void bw_DoWork( object sender, DoWorkEventArgs e) { DependencyObj.Dispatcher.BeginInvoke( new Action(() => { wattingDialog = new WattingDialog(); wattingDialog.ShowWattingDialog(); })); DoWorkAction(); } public static void ShowWaittingDialog(FrameworkElement dObj,Action DoWork) { WattingHelper wattingHelper= new WattingHelper (dObj,DoWork); } } |
调用方法。
WattingHelper.ShowWaittingDialog( this , () => { //耗时操作。 }); |
最后结果。
------------------------------------------------------------
如非注明都是原创,如需转载请注出处。
如非注明都是原创,如需转载请注出处。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥