【WPF】 实现过渡弹框

 前言

  在导出Excel时候 可能需要一个弹框来提示用户 导出以完成的 功能 可是要怎么做呢?

  这里有个方法可以实现,做个笔记 方便以后的快速调用,也希望你能有所收获

 内容

前台界面(XMAL):

View Code
<Grid x:Name="gridDialog" Visibility="Hidden">

                    <Canvas Background="White" Opacity="0.7" x:Name="canvasMask"></Canvas>

                    <Border x:Name="borderShadow" SnapsToDevicePixels="True" Width="230" Height="80" Grid.ColumnSpan="2"  BorderBrush="LightGray" BorderThickness="0.5" CornerRadius="5" Background="White">

                        <Border.Effect>

                            <DropShadowEffect Color="Black" BlurRadius="16" ShadowDepth="0" Opacity="0.5" />

                        </Border.Effect>

                    </Border>

                    <Grid Width="220" Height="70" x:Name="gridResult">

                        <Grid.ColumnDefinitions>

                            <ColumnDefinition Width="80"></ColumnDefinition>

                            <ColumnDefinition Width="*"></ColumnDefinition>

                        </Grid.ColumnDefinitions>

                        

                        <Image Grid.Column="0" Source="../Images/success.png" Height="48" Width="48"></Image>

                        <TextBlock Grid.Column="1" x:Name="textBlockDisplay" VerticalAlignment="Center" Text="导出成功!"></TextBlock>

                    </Grid>

                    <my:ProgressBar x:Name="loading" Visibility="Collapsed"  Grid.Column="0"></my:ProgressBar>

                </Grid>

PS: Canvas 画布,设置透明色 起遮罩层的作用
    Border 边框
    textBlockDisplay 为显示的内容 后台可以自定义

后台代码(.CS):

View Code
/// <summary>

        /// 显示正在执行结果界面

        /// </summary>

        /// <param name="resultText">执行结果界面提示文字</param>

        private void ShowDialog(string resultText)



        {

            gridDialog.Visibility = System.Windows.Visibility.Visible;

            borderShadow.Visibility = System.Windows.Visibility.Visible;

            gridResult.Visibility = System.Windows.Visibility.Visible;



            loading.Visibility = System.Windows.Visibility.Hidden;



            textBlockDisplay.Text = resultText;



            //计算位置使显示居中

            gridDialog.Margin = new Thickness(0, scrollViewer1.VerticalOffset-50, 0, 0);//scrollViewer1.VerticalOffset为滚动过的位置

            gridDialog.Height = scrollViewer1.ViewportHeight+100;



            DispatcherTimer timer = new DispatcherTimer();

            timer.Interval = TimeSpan.FromSeconds(1d);

            timer.Tick += (sender, e) =>

            {

                gridDialog.Visibility = System.Windows.Visibility.Hidden;

            };

            timer.Start();

        }

//PS:由于使用到了滚动条所以才有了 设置高度的 那个动作 这个看实际需求来进行修改



 调用:

   TaskScheduler scheduler = TaskScheduler.FromCurrentSynchronizationContext();

                    CancellationToken token = new CancellationToken();

                    Task.Factory.StartNew(new Action(() =>



{

//里面写你的实际需求  如导出Excel的代码

})).ContinueWith(w => { ShowDialog("导出Excel成功!"); }, token, TaskContinuationOptions.None, scheduler);



//PS:调用中使用到了线程中的异步操作 这部分不是很懂 调用的片段可以放到  按钮触发事件

  结果
  实现的效果如下

        

 

你可能感兴趣的:(WPF)