WPF加载中实现

2020-07-27 17:23:20 浏览数 (1)

加载页面

XAML

代码语言:javascript复制
<Window
    x:Class="SchoolClient.Wins.LoadDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="100"
    Height="100"
    AllowsTransparency="True"
    Background="Transparent"
    Opacity="1"
    ResizeMode="NoResize"
    Topmost="True"
    WindowStartupLocation="CenterScreen"
    WindowStyle="None">
    <Grid>
        <Border Background="#66000000" CornerRadius="10 10 10 10">
            <Grid Width="60" Height="60">
                <Grid.Resources>
                    <DrawingBrush
                        x:Key="brush"
                        AlignmentX="Center"
                        AlignmentY="Top"
                        Stretch="None">
                        <DrawingBrush.Drawing>
                            <GeometryDrawing Brush="White">
                                <GeometryDrawing.Geometry>
                                    <EllipseGeometry RadiusX="2" RadiusY="5" />
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>
                        </DrawingBrush.Drawing>
                    </DrawingBrush>
                </Grid.Resources>

                <Rectangle
                    x:Name="r01"
                    Fill="{StaticResource brush}"
                    Opacity="0.8"
                    RenderTransformOrigin="0.5,0.5">
                    <Rectangle.RenderTransform>
                        <RotateTransform Angle="0" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Rectangle
                    x:Name="r02"
                    Fill="{StaticResource brush}"
                    Opacity="0.8"
                    RenderTransformOrigin="0.5,0.5">
                    <Rectangle.RenderTransform>
                        <RotateTransform Angle="30" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Rectangle
                    x:Name="r03"
                    Fill="{StaticResource brush}"
                    Opacity="0.8"
                    RenderTransformOrigin="0.5,0.5">
                    <Rectangle.RenderTransform>
                        <RotateTransform Angle="60" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Rectangle
                    x:Name="r04"
                    Fill="{StaticResource brush}"
                    Opacity="0.8"
                    RenderTransformOrigin="0.5,0.5">
                    <Rectangle.RenderTransform>
                        <RotateTransform Angle="90" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Rectangle
                    x:Name="r05"
                    Fill="{StaticResource brush}"
                    Opacity="0.8"
                    RenderTransformOrigin="0.5,0.5">
                    <Rectangle.RenderTransform>
                        <RotateTransform Angle="120" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Rectangle
                    x:Name="r06"
                    Fill="{StaticResource brush}"
                    Opacity="0.8"
                    RenderTransformOrigin="0.5,0.5">
                    <Rectangle.RenderTransform>
                        <RotateTransform Angle="150" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Rectangle
                    x:Name="r07"
                    Fill="{StaticResource brush}"
                    Opacity="0.8"
                    RenderTransformOrigin="0.5,0.5">
                    <Rectangle.RenderTransform>
                        <RotateTransform Angle="180" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Rectangle
                    x:Name="r08"
                    Fill="{StaticResource brush}"
                    Opacity="0.8"
                    RenderTransformOrigin="0.5,0.5">
                    <Rectangle.RenderTransform>
                        <RotateTransform Angle="210" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Rectangle
                    x:Name="r09"
                    Fill="{StaticResource brush}"
                    Opacity="0.8"
                    RenderTransformOrigin="0.5,0.5">
                    <Rectangle.RenderTransform>
                        <RotateTransform Angle="240" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Rectangle
                    x:Name="r10"
                    Fill="{StaticResource brush}"
                    Opacity="0.8"
                    RenderTransformOrigin="0.5,0.5">
                    <Rectangle.RenderTransform>
                        <RotateTransform Angle="270" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Rectangle
                    x:Name="r11"
                    Fill="{StaticResource brush}"
                    Opacity="0.8"
                    RenderTransformOrigin="0.5,0.5">
                    <Rectangle.RenderTransform>
                        <RotateTransform Angle="300" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Rectangle
                    x:Name="r12"
                    Fill="{StaticResource brush}"
                    Opacity="0.8"
                    RenderTransformOrigin="0.5,0.5">
                    <Rectangle.RenderTransform>
                        <RotateTransform Angle="330" />
                    </Rectangle.RenderTransform>
                </Rectangle>
                <Grid.Triggers>
                    <EventTrigger RoutedEvent="Grid.Loaded">
                        <BeginStoryboard>
                            <Storyboard RepeatBehavior="Forever">
                                <DoubleAnimation
                                    AutoReverse="True"
                                    BeginTime="0:0:0.00000"
                                    Storyboard.TargetName="r01"
                                    Storyboard.TargetProperty="Opacity"
                                    To="0"
                                    Duration="0:0:0.08333" />
                                <DoubleAnimation
                                    AutoReverse="True"
                                    BeginTime="0:0:0.08333"
                                    Storyboard.TargetName="r02"
                                    Storyboard.TargetProperty="Opacity"
                                    To="0"
                                    Duration="0:0:0.08333" />
                                <DoubleAnimation
                                    AutoReverse="True"
                                    BeginTime="0:0:0.16666"
                                    Storyboard.TargetName="r03"
                                    Storyboard.TargetProperty="Opacity"
                                    To="0"
                                    Duration="0:0:0.08333" />
                                <DoubleAnimation
                                    AutoReverse="True"
                                    BeginTime="0:0:0.24999"
                                    Storyboard.TargetName="r04"
                                    Storyboard.TargetProperty="Opacity"
                                    To="0"
                                    Duration="0:0:0.08333" />
                                <DoubleAnimation
                                    AutoReverse="True"
                                    BeginTime="0:0:0.33332"
                                    Storyboard.TargetName="r05"
                                    Storyboard.TargetProperty="Opacity"
                                    To="0"
                                    Duration="0:0:0.08333" />
                                <DoubleAnimation
                                    AutoReverse="True"
                                    BeginTime="0:0:0.41665"
                                    Storyboard.TargetName="r06"
                                    Storyboard.TargetProperty="Opacity"
                                    To="0"
                                    Duration="0:0:0.08333" />
                                <DoubleAnimation
                                    AutoReverse="True"
                                    BeginTime="0:0:0.49998"
                                    Storyboard.TargetName="r07"
                                    Storyboard.TargetProperty="Opacity"
                                    To="0"
                                    Duration="0:0:0.08333" />
                                <DoubleAnimation
                                    AutoReverse="True"
                                    BeginTime="0:0:0.58331"
                                    Storyboard.TargetName="r08"
                                    Storyboard.TargetProperty="Opacity"
                                    To="0"
                                    Duration="0:0:0.08333" />
                                <DoubleAnimation
                                    AutoReverse="True"
                                    BeginTime="0:0:0.66664"
                                    Storyboard.TargetName="r09"
                                    Storyboard.TargetProperty="Opacity"
                                    To="0"
                                    Duration="0:0:0.08333" />
                                <DoubleAnimation
                                    AutoReverse="True"
                                    BeginTime="0:0:0.74997"
                                    Storyboard.TargetName="r10"
                                    Storyboard.TargetProperty="Opacity"
                                    To="0"
                                    Duration="0:0:0.08333" />
                                <DoubleAnimation
                                    AutoReverse="True"
                                    BeginTime="0:0:0.83330"
                                    Storyboard.TargetName="r11"
                                    Storyboard.TargetProperty="Opacity"
                                    To="0"
                                    Duration="0:0:0.08333" />
                                <DoubleAnimation
                                    AutoReverse="True"
                                    BeginTime="0:0:0.91663"
                                    Storyboard.TargetName="r12"
                                    Storyboard.TargetProperty="Opacity"
                                    To="0"
                                    Duration="0:0:0.08333" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Grid.Triggers>
            </Grid>
        </Border>
    </Grid>
</Window>

C#

代码语言:javascript复制
using System.Windows;

namespace SchoolClient.Wins
{
    /// <summary>
    /// LoadDialog.xaml 的交互逻辑
    /// </summary>
    public partial class LoadDialog : Window
    {
        public LoadDialog()
        {
            InitializeComponent();
        }
    }
}

使用方式

定义自己的APP类

代码语言:javascript复制
internal class MyApp : Application
{
    public static LoadDialog myloading;

    [STAThread]
    private static void Main()

    {
        // 定义Application对象作为整个应用程序入口
        Application app = new Application();
        LoginWindow win = new LoginWindow();
        myloading = new LoadDialog();
        app.Run(win);
    }
}

显示

代码语言:javascript复制
MyApp.myloading.Show();

隐藏

代码语言:javascript复制
MyApp.myloading.Hide();

0 人点赞