WPF Loading等待线程调用

<Window x:Class="RecCore.Main.Controls.Loading.LoadingCtl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:RecCore.Main.Controls.Loading"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        ResizeMode="NoResize"
        WindowStyle="None"
        Background="Transparent"
        BorderThickness="0"
        AllowsTransparency="True"
        d:DesignHeight="3000" d:DesignWidth="3000" Width="120" Height="120">
    <Grid Background="Transparent" Name="LayoutRoot">
        <Grid.RenderTransform>
            <ScaleTransform x:Name="SpinnerScale" ScaleX="1.0" ScaleY="1.0" />
        </Grid.RenderTransform>
        <TextBlock Text="Loading..."  HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="14" Foreground="Orange" FontWeight="Bold" />
        <Canvas Height="120" HorizontalAlignment="Center" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Center" Width="120">
            <Canvas.RenderTransform>
                <RotateTransform x:Name="SpinnerRotate" Angle="0" />
            </Canvas.RenderTransform>
            <Canvas.Triggers>
                <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                    <BeginStoryboard>
                        <BeginStoryboard.Storyboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:01" From="0" RepeatBehavior="Forever" Storyboard.TargetName="SpinnerRotate" Storyboard.TargetProperty="(RotateTransform.Angle)" To="360" />
                            </Storyboard>
                        </BeginStoryboard.Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Canvas.Triggers>
            <Ellipse Canvas.Left="47.2783" Canvas.Top="0.5" Fill="Blue" Height="21.862" Opacity="1" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="20.1696" Canvas.Top="9.76358" Fill="Blue" Height="21.862" Opacity="0.9" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="2.86816" Canvas.Top="29.9581" Fill="Blue" Height="21.862" Opacity="0.8" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="5.03758e-006" Canvas.Top="57.9341" Fill="Blue" Height="21.862" Opacity="0.7" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="12.1203" Canvas.Top="83.3163" Fill="Blue" Height="21.862" Opacity="0.6" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="36.5459" Canvas.Top="98.138" Fill="Blue" Height="21.862" Opacity="0.5" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="64.6723" Canvas.Top="96.8411" Fill="Blue" Height="21.862" Opacity="0.4" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="87.6176" Canvas.Top="81.2783" Fill="Blue" Height="21.862" Opacity="0.3" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="98.165" Canvas.Top="54.414" Fill="Blue" Height="21.862" Opacity="0.2" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="92.9838" Canvas.Top="26.9938" Fill="Blue" Height="21.862" Opacity="0.1" Stretch="Fill" Width="21.835" />
        </Canvas>
    </Grid>
</Window>

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace RecCore.Main.Controls.Loading
{
    public class LoadingHelper
    {
        private static LoadingCtl loadingCtl;

        public static void ShowLoading()
        {
            Thread thread = new Thread(new ThreadStart(ShowWPF));
            thread.IsBackground = true;
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }

        /// <summary>
        /// 开启loading
        /// </summary>
        private static void ShowWPF()
        {
            if (loadingCtl != null)
            {
                loadingCtl.Close();
                loadingCtl = null;
            }
            loadingCtl = new LoadingCtl();
            loadingCtl.Topmost = true;
            loadingCtl.ShowDialog();
        }

        /// <summary>
        /// 关闭loading
        /// </summary>
        public static void CloseLoading()
        {
            Thread.Sleep(50);
            if (loadingCtl != null)
            {
                Thread.Sleep(50);
                if (loadingCtl != null)
                {
                    Thread.Sleep(50);
                    loadingCtl.Dispatcher.InvokeAsync(() =>
                    {
                        loadingCtl.Close();
                        loadingCtl = null;
                    });
                }
            }
        }
    }
}

  

原文地址:https://www.cnblogs.com/Mzg121584668/p/15047964.html