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()));}
}

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/46129.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

35.UART(通用异步收发传输器)-RS232(2)

&#xff08;1&#xff09;RS232接收模块visio框图&#xff1a; &#xff08;2&#xff09;接收模块Verilog代码编写: /* 常见波特率&#xff1a; 4800、9600、14400、115200 在系统时钟为50MHz时&#xff0c;对应计数为&#xff1a; (1/4800) * 10^9 /20 -1 10416 …

【作业】 贪心算法1

Tips:三题尚未完成。 #include <iostream> #include <algorithm> using namespace std; int a[110]; int main(){int n,r,sum0;cin>>n>>r;for(int i0;i<n;i){cin>>a[i];}sort(a0,an);for(int i0;i<n;i){if(i>r){a[i]a[i-r]a[i];}suma[…

[USACO18JAN] Cow at Large P

题解都说了&#xff0c;当统计 u u u为根节点的时候&#xff0c;答案就是满足以下条件的 i i i的数量&#xff1a; d i ≥ g i d_i≥g_i di​≥gi​且 d f a i < g f a i d_{fa_i}<g_{fa_i} dfai​​<gfai​​&#xff0c;设这个数量为 a n s ans ans。以下严格证明 …

Solana开发资源都有哪些

Solana是一个高性能的区块链平台&#xff0c;吸引了大量开发者构建去中心化应用&#xff08;dApps&#xff09;。以下是一些有用的Solana开发教程和资源&#xff1a; 官方资源 Solana 官方文档&#xff1a; Solana Documentation: 这是最全面的资源&#xff0c;包括快速入门、…

[实践篇]13.29 QNX下的系统性能监控工具 - sysMonAppQNX(二)

2.7 getinfo: 获取 DSP 的详细信息 getinfo 可用选项 (如果没有参数,则使用默认值) --q6 (默认选择处理器: ADSP): adsp - 选择的处理器为 ADSPsdsp - 选择的处理器为传感器 DSPcdsp - 选择的处理器为计算 DSP示例: ./sysMonApp getinfo --q6 cdsp - 获取计算 DSP 的详细信…

大气热力学(8)——热力学图的应用之一(气象要素求解)

本篇文章源自我在 2021 年暑假自学大气物理相关知识时手写的笔记&#xff0c;现转化为电子版本以作存档。相较于手写笔记&#xff0c;电子版的部分内容有补充和修改。笔记内容大部分为公式的推导过程。 文章目录 8.1 复习斜 T-lnP 图上的几种线8.1.1 等温线和等压线8.1.2 干绝热…

连锁零售门店分析思路-人货场 数据分析

连锁零售门店分析思路 以下是一个连锁零售门店的分析思路&#xff1a; 一、市场与竞争分析 二、门店运营分析&#xff08;销售分析&#xff09; 三、销售与财务分析 四、客户分析 五、数字化与营销分析 最近帮一个大学生培训&#xff0c;就门店销售分析 &#xff0c;说到门店…

使用windows批量解压和布局ImageNet ISLVRC2012数据集

使用的系统是windows&#xff0c;找到的解压命令很多都linux系统中的&#xff0c;为了能在windows系统下使用&#xff0c;因此下载Git这个软件&#xff0c;在其中的Git Bash中使用以下命令&#xff0c;因为Git Bash集成了很多linux的命令&#xff0c;方便我们的使用。 ImageNe…

[iOS]类和对象的底层原探索

[iOS]类和对象的底层探索 文章目录 [iOS]类和对象的底层探索继承链&#xff08;类&#xff0c;父类&#xff0c;元类&#xff09;instance 实例对象class 类对象meta-class 元类对象 对对象、类、元类和分类的探索instance 实例对象class 类对象meta-class 元类对象分类(catego…

react项目使用EventBus实现登录拦截

关于EventBus EventBus是一个事件发布/订阅模式的实现&#xff0c;它允许不同的组件或模块之间进行通信&#xff0c;而不需要直接引用对方。这种模式特别适用于那些需要跨组件传递信息&#xff0c;但又不想引入复杂依赖关系的场景。 实现思路 利用axios中间件&#xff0c;在…

防火墙之带宽管理篇

核心思想 1.带宽限制&#xff1a;限制非关键业务流量占用带宽的比例 2.带宽保证&#xff1a;保证关键的业务流量传输不受影响。业务繁忙时&#xff0c;确保业务不受影响。 3.限制连接数&#xff1a;可以针对某些业务进行连接数的限制&#xff0c;首先可以降低该业务占用带宽…

基于UltraFace的人脸检测在地平线旭日X3派上的部署和测试(Python版本和C++版本)

电脑端的测试环境搭建 如果不想再搭建环境和测试代码bug上浪费更多的时间可以直接获取本人的测试虚拟机&#xff0c;所有的测试代码、虚拟环境和板端测试工程以全部打包到了虚拟机&#xff0c;需要的可以通过网盘获取&#xff1a; 代码和虚拟机百度网盘链接&#xff1a; 链接…

【AI绘画教程】Stable Diffusion 1.5 vs 2

在本文中,我们将总结稳定扩散 1 与稳定扩散 2 辩论中的所有要点。我们将在第一部分中查看这些差异存在的实际原因,但如果您想直接了解实际差异,您可以跳下否定提示部分。让我们开始吧! Stable Diffusion 2.1 发布与1.5相比,2.1旨在解决2.0的许多相对缺点。本文的内容与理解…

网络和安全操作

一、编辑文件 文本编辑器有很多&#xff0c;比如图形模式的gedit、OpenOffice 等&#xff0c;文本模式下的编辑器有vi、vim&#xff08;vi的增强版本&#xff09;等。vi和vim是我们在Linux中最常用的编辑器。 gedit&#xff1a;类似于windows下的记事本&#xff0c;很方便的去…

IO多路复用技术、select、poll、epoll联系与区别

目录 IO多路复用技术select&#xff1a;poll&#xff1a;epoll&#xff08;Linux特有&#xff09;&#xff1a; epoll select poll的区别epoll是同步还是异步epoll详解 IO多路复用技术 通信双方都有一个socket&#xff0c;以一个文件描述符的形式存在&#xff0c;那这个fd也对…

AI 大事件:超级明星 Andrej Karpathy 创立AI教育公司 Eureka Labs

&#x1f9e0; AI 大事件&#xff1a;超级明星 Andrej Karpathy 创立AI教育公司 Eureka Labs 摘要 Andrej Karpathy 作为前 OpenAI 联合创始人、Tesla AI 团队负责人&#xff0c;他的专业性和实力备受瞩目。Karpathy 对 AI 的普及和教育充满热情&#xff0c;从 YouTube 教程到…

CBSD bhyve Ubuntu 配置vnc登录管理

CBSD介绍 CBSD是为FreeBSD jail子系统、bhyve、QEMU/NVMM和Xen编写的管理层。该项目定位为一个综合解决方案的单一集成工具&#xff0c;用于使用预定义的软件集以最少的配置快速构建和部署计算机虚拟环境。 虽然CBSD没有提供额外的操作系统级功能&#xff0c;但它极大地简化了…

两年经验前端带你重学前端框架必会的ajax+node.js+webpack+git等技术 Day1

黑马程序员前端AJAX入门到实战全套教程&#xff0c;包含学前端框架必会的&#xff08;ajaxnode.jswebpackgit&#xff09;&#xff0c;一套全覆盖 Day1 你好,我是Qiuner. 为帮助别人少走弯路和记录自己编程学习过程而写博客 这是我的 github https://github.com/Qiuner ⭐️ ​…

【算法/天梯赛训练】天梯赛模拟题集

L1-009 N个数求和 #include <iostream> #include <algorithm>using namespace std;typedef long long ll; const int N 105;typedef struct node {ll x, y; }node; node a[N];ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a; }int main() {int n;cin >>…

《昇思25天学习打卡营第25天|第9天》

今天是打卡的第九天&#xff0c;今天学习的是使用静态图加速这门课程&#xff0c;从他的背景学起&#xff1a;AI编译框架分为两种运行模式&#xff0c;分别是动态图模式和静态图模式&#xff0c;动态图模式特点&#xff1a;计算图的构建和计算同时发生&#xff0c;缺点&#xf…