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.pswp.cn/web/46129.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/46129.shtml
英文地址,請注明出處:http://en.pswp.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…