WPF设置欢迎屏幕,程序启动过度动画

当主窗体加载时间过长,这时候基本都会想添加一个等待操作来响应用户点击,提高用户体验。下面我记录两个方法,一点拙见,仅供参考。

方法1:在App类中使用SplashScreen类。

protected override void OnStartup(StartupEventArgs e)
{
    // 显示启动画面
    ShowSplashScreen();
    base.OnStartup(e);
}

private void ShowSplashScreen()
{
    // 这里要注意的点就是aa.jpg 属性的生成的操作要设置为资源(Resource).
    SplashScreen splash = new SplashScreen(/Images/aa.jpg);
    /*
     * 第一个参数表示是否自动关闭
     * 第二个参数表示启动画面窗口是否会被置于顶层,即使有其他程序的窗口处于活动状态,
     * 启动画面也会显示在它们之上.
     * 如果设置为false,表示遵循正常的窗口焦点规则,如果用户切换到其他程序,
     * 启动画面可能会被其他窗口遮挡.
     * 如果设置为true,保证用户一直能够看到它.
     */
    splash.Show(true, true);
    // 这里执行一些数据初始化的代码
    //Task.Run(() =>
    //{
    //    // 模拟耗时操作
    //    Thread.Sleep(1000);

    //    // 在UI线程上关闭启动画面
    //    Dispatcher.Invoke(() =>
    //    {
    //        // 这里会有一个淡淡渐渐消失的效果
    //        splash.Close(TimeSpan.FromSeconds(2));
    //    });
    //});
}

也可以直接设置图片的属性-生成的操作一栏为SplashScreen。这样就不用写上面的代码。

方法1缺点:仅能展示图片,gif图片也仅展示第一帧,如有办法展示完整gif请给个传送门。

方法2:自定义窗体作为欢迎屏幕

参考窗体代码(后台无操作,故仅贴前台代码,带加载动画的哦):我命名为:Splash

<Window x:Class="SmartHome.Splash"
	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" mc:Ignorable="d"
    Title="Splash" Width="640" Height="480" Background="#FF1D2128"  ResizeMode="NoResize"
 ShowInTaskbar="False"
 SnapsToDevicePixels="True"
 TextOptions.TextFormattingMode="Display"
 UseLayoutRounding="True"
 WindowStartupLocation="CenterScreen"
 WindowStyle="None" Foreground="{x:Null}">
    <Grid>
        <Rectangle HorizontalAlignment="Left" Height="60" Margin="173,117,0,0" Stroke="Black" VerticalAlignment="Top" Width="60" Fill="#FF2573DC" RadiusX="10" RadiusY="10"/>
        <Rectangle Fill="#FF1D2128" HorizontalAlignment="Left" Height="43" Margin="185.332,125.671,0,0" RadiusY="5" RadiusX="5" Stroke="#FF1D2128" VerticalAlignment="Top" Width="10"/>
        <Rectangle Fill="#FF1D2128" HorizontalAlignment="Left" Height="27.046" Margin="210.749,141.625,0,0" RadiusY="5" RadiusX="5" Stroke="#FF1D2128" VerticalAlignment="Top" Width="10"/>
        <Rectangle Fill="#FF1D2128" HorizontalAlignment="Left" Height="10" Margin="189.25,141.625,0,0" RadiusY="5" RadiusX="5" Stroke="#FF1D2128" VerticalAlignment="Top" Width="31.499"/>
        <Ellipse Fill="#FF1D2128" HorizontalAlignment="Left" Height="11" Margin="209.749,125.671,0,0" Stroke="#FF1D2128" VerticalAlignment="Top" Width="11"/>
        <Path Data="M244,102 L244,187.2115" Fill="#FF2573DC" HorizontalAlignment="Left" Height="60" Margin="250,117,0,0" Stretch="Fill" Stroke="#FF2573DC" VerticalAlignment="Top" Width="3" StrokeThickness="3"/>
        <Label Content="HAHAHAHA" Height="60" Margin="259,115,169,0" VerticalAlignment="Top" Foreground="#FF2573DC" FontSize="48" FontFamily="French Script MT" FontWeight="Bold"/>
        <Path Data="M1.5000012,1.5 L14.500009,14.500008 M14.500005,12.39 L1.5,25.390005" Fill="#FF2573DC" HorizontalAlignment="Left" Margin="205.167,230.25,0,222.86" Stretch="Fill" Stroke="#FF2573DC" StrokeThickness="3" Width="16"/>
        <Label Content="程序名称" Margin="230.749,221,204,222.86" FontSize="24" Foreground="White"/>
        <Label Content="即将进入" Margin="278.749,0,277,182.86" FontSize="16" Foreground="White" Height="36.14" HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
        <Grid Background="#FF1D2128"  HorizontalAlignment="Left" Margin="249,301,0,99">
            <Grid.Resources>
                <Style x:Key="rec" TargetType="Rectangle">
                    <Setter Property="Width" Value="10"/>
                    <Setter Property="Height" Value="30"/>
                    <Setter Property="Fill" Value="#FF2573DC"/>
                </Style>
                <PowerEase x:Key="powerEase" Power="3" EasingMode="EaseInOut"/>
            </Grid.Resources>
            <Grid.Triggers>
                <EventTrigger RoutedEvent="Loaded">
                    <BeginStoryboard>
                        <Storyboard RepeatBehavior="Forever" Storyboard.TargetProperty="Height">
                            <DoubleAnimation Storyboard.TargetName="rec1" To="50" BeginTime="0:0:0.0" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/>
                            <DoubleAnimation Storyboard.TargetName="rec2" To="50" BeginTime="0:0:0.1" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/>
                            <DoubleAnimation Storyboard.TargetName="rec3" To="50" BeginTime="0:0:0.2" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/>
                            <DoubleAnimation Storyboard.TargetName="rec4" To="50" BeginTime="0:0:0.3" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/>
                            <DoubleAnimation Storyboard.TargetName="rec5" To="50" BeginTime="0:0:0.4" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/>
                            <DoubleAnimation Storyboard.TargetName="rec6" To="50" BeginTime="0:0:0.5" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/>
                            <DoubleAnimation Storyboard.TargetName="rec7" To="50" BeginTime="0:0:0.6" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/>
                            <DoubleAnimation Storyboard.TargetName="rec8" To="50" BeginTime="0:0:0.7" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/>
                            <DoubleAnimation Storyboard.TargetName="rec9" To="50" BeginTime="0:0:0.8" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/>
                            <DoubleAnimation Storyboard.TargetName="rec10" To="50" BeginTime="0:0:0.9" Duration="0:0:0.5" EasingFunction="{StaticResource powerEase}" AutoReverse="True"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Grid.Triggers>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="15"/>
                <ColumnDefinition Width="15"/>
                <ColumnDefinition Width="15"/>
                <ColumnDefinition Width="15"/>
                <ColumnDefinition Width="15"/>
                <ColumnDefinition Width="15"/>
                <ColumnDefinition Width="15"/>
                <ColumnDefinition Width="15"/>
                <ColumnDefinition Width="15"/>
                <ColumnDefinition Width="15"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Label Content="Loading..."
                   Grid.Row="1"
           FontSize="18"
           FontFamily="Times New Roman"
           FontWeight="Bold"
           Grid.ColumnSpan="10"
           VerticalContentAlignment="Center"
           HorizontalContentAlignment="Center"/>
            <Rectangle Name="rec1" Grid.Column="0" Style="{StaticResource rec}"/>
            <Rectangle Name="rec2" Grid.Column="1" Style="{StaticResource rec}"/>
            <Rectangle Name="rec3" Grid.Column="2" Style="{StaticResource rec}"/>
            <Rectangle Name="rec4" Grid.Column="3" Style="{StaticResource rec}"/>
            <Rectangle Name="rec5" Grid.Column="4" Style="{StaticResource rec}"/>
            <Rectangle Name="rec6" Grid.Column="5" Style="{StaticResource rec}"/>
            <Rectangle Name="rec7" Grid.Column="6" Style="{StaticResource rec}"/>
            <Rectangle Name="rec8" Grid.Column="7" Style="{StaticResource rec}"/>
            <Rectangle Name="rec9" Grid.Column="8" Style="{StaticResource rec}"/>
            <Rectangle Name="rec10" Grid.Column="9" Style="{StaticResource rec}"/>
        </Grid>
    </Grid>
</Window>

使用方式:

在APP类下设置新的单线程打开窗体并赋值给全局变量方便关闭:

//定义一个静态的全局欢迎窗口
public static Splash Splash = new Splash();
protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    Thread t = new Thread(() =>
    {
        Splash splash = new Splash();
        //把实例赋值给全局变量
        Splash= splash;
        //用Show的话动画效果不流畅
        splash.ShowDialog();
    });
    t.Name = "HelloWindow";
    //设置为单线程。一定要设置
    t.SetApartmentState(ApartmentState.STA);
    t.Start();
}

主窗体启动成功后的关闭方法:

public MainWindow()
{
    InitializeComponent();
//耗时的操作,比如实例化控件,页面,初始化数据等
//...
//窗口内容呈现完成后的方法,用load方法也可以看自己
this.ContentRendered += MainWindow_ContentRendered;
}

private void MainWindow_ContentRendered(object sender, EventArgs e)
{
    if (App.Splash != null)
    {
        //在该线程上关闭
        App.Splash.Dispatcher.Invoke((Action)(() => App.Splash.Close()));
    }
}

一点拙见,还请指正。各位大佬有更好的方法还请留个传送门,或评论区展现一下风采!!!!

好记性不如烂笔头................................................................................................

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用WPF动画和线程来实现这个功能。以下是一个简单的实现方法: 1. 在启动窗口中,添加一个动画控件,比如一个旋转的圆圈。 2. 启动一个线程,在后台检测连接是否成功。 3. 当连接成功时,关闭启动窗口,打开应用程序主窗口。 以下是一个示例代码: XAML代码: ```xml <Window x:Class="StartupWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:YourNamespace" Title="StartupWindow" Height="300" Width="300"> <Grid> <Canvas> <Ellipse Width="50" Height="50" Fill="Blue"> <Ellipse.RenderTransform> <RotateTransform Angle="0" CenterX="25" CenterY="25"/> </Ellipse.RenderTransform> <Ellipse.Triggers> <EventTrigger RoutedEvent="Loaded"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="(Ellipse.RenderTransform).(RotateTransform.Angle)" From="0" To="360" Duration="0:0:1" RepeatBehavior="Forever"/> </Storyboard> </BeginStoryboard> </EventTrigger> </Ellipse.Triggers> </Ellipse> </Canvas> </Grid> </Window> ``` C# 代码: ```csharp public partial class StartupWindow : Window { public StartupWindow() { InitializeComponent(); // 启动后台线程 Thread thread = new Thread(new ThreadStart(CheckConnection)); thread.Start(); } private void CheckConnection() { // 检测连接是否成功 bool isConnected = YourNamespace.CheckConnection(); // 如果连接成功,关闭启动窗口,打开主窗口 if (isConnected) { Dispatcher.Invoke(() => { MainWindow mainWindow = new MainWindow(); mainWindow.Show(); Close(); }); } } } ``` 在上面的代码中,我们使用了Canvas和Ellipse来创建一个圆圈动画。我们使用了一个后台线程来检测连接是否成功,如果连接成功,我们使用Dispatcher.Invoke方法在主线程中关闭启动窗口,打开主窗口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值