zoukankan      html  css  js  c++  java
  • 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, () =>
                {
    
                                //耗时操作。
    
              });
    
    

    最后结果。

    ------------------------------------------------------------
    如非注明都是原创,如需转载请注出处。
  • 相关阅读:
    SQL Server IF Exists 判断数据库对象是否存在的用法
    C# RDLC报表不出现预览窗体直接输出到打印机
    C# 未安装Office环境下使用NPOI导出Excel文件
    C# 键盘中的按键对应KeyValue
    微信小程序下可以使用的MD5以及AES加密(通用)
    SQL Server 根据树状结构表生成以/号分割的路由字符串
    C# Winform下一个热插拔的MIS/MRP/ERP框架16(窗体基类场景2)
    WEB H5 JS QRCode二维码快速自动生成
    C# Winform 小技巧(Datagridview某一列按状态显示不同图片)
    获取请求地址的IP地址
  • 原文地址:https://www.cnblogs.com/Ivan83/p/2033075.html
Copyright © 2011-2022 走看看