用流媒体做的电台网站,网大论坛,公司注册费用计入什么科目,公路投资建设有限公司网站当主窗体加载时间过长#xff0c;这时候基本都会想添加一个等待操作来响应用户点击#xff0c;提高用户体验。下面我记录两个方法#xff0c;一点拙见#xff0c;仅供参考。
方法1:在App类中使用SplashScreen类。
protected override void OnStartup(StartupEventArgs e)…当主窗体加载时间过长这时候基本都会想添加一个等待操作来响应用户点击提高用户体验。下面我记录两个方法一点拙见仅供参考。
方法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:ClassSmartHome.Splashxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 mc:IgnorabledTitleSplash Width640 Height480 Background#FF1D2128 ResizeModeNoResizeShowInTaskbarFalseSnapsToDevicePixelsTrueTextOptions.TextFormattingModeDisplayUseLayoutRoundingTrueWindowStartupLocationCenterScreenWindowStyleNone Foreground{x:Null}GridRectangle HorizontalAlignmentLeft Height60 Margin173,117,0,0 StrokeBlack VerticalAlignmentTop Width60 Fill#FF2573DC RadiusX10 RadiusY10/Rectangle Fill#FF1D2128 HorizontalAlignmentLeft Height43 Margin185.332,125.671,0,0 RadiusY5 RadiusX5 Stroke#FF1D2128 VerticalAlignmentTop Width10/Rectangle Fill#FF1D2128 HorizontalAlignmentLeft Height27.046 Margin210.749,141.625,0,0 RadiusY5 RadiusX5 Stroke#FF1D2128 VerticalAlignmentTop Width10/Rectangle Fill#FF1D2128 HorizontalAlignmentLeft Height10 Margin189.25,141.625,0,0 RadiusY5 RadiusX5 Stroke#FF1D2128 VerticalAlignmentTop Width31.499/Ellipse Fill#FF1D2128 HorizontalAlignmentLeft Height11 Margin209.749,125.671,0,0 Stroke#FF1D2128 VerticalAlignmentTop Width11/Path DataM244,102 L244,187.2115 Fill#FF2573DC HorizontalAlignmentLeft Height60 Margin250,117,0,0 StretchFill Stroke#FF2573DC VerticalAlignmentTop Width3 StrokeThickness3/Label ContentHAHAHAHA Height60 Margin259,115,169,0 VerticalAlignmentTop Foreground#FF2573DC FontSize48 FontFamilyFrench Script MT FontWeightBold/Path DataM1.5000012,1.5 L14.500009,14.500008 M14.500005,12.39 L1.5,25.390005 Fill#FF2573DC HorizontalAlignmentLeft Margin205.167,230.25,0,222.86 StretchFill Stroke#FF2573DC StrokeThickness3 Width16/Label Content程序名称 Margin230.749,221,204,222.86 FontSize24 ForegroundWhite/Label Content即将进入 Margin278.749,0,277,182.86 FontSize16 ForegroundWhite Height36.14 HorizontalAlignmentCenter VerticalAlignmentBottom/Grid Background#FF1D2128 HorizontalAlignmentLeft Margin249,301,0,99Grid.ResourcesStyle x:Keyrec TargetTypeRectangleSetter PropertyWidth Value10/Setter PropertyHeight Value30/Setter PropertyFill Value#FF2573DC//StylePowerEase x:KeypowerEase Power3 EasingModeEaseInOut//Grid.ResourcesGrid.TriggersEventTrigger RoutedEventLoadedBeginStoryboardStoryboard RepeatBehaviorForever Storyboard.TargetPropertyHeightDoubleAnimation Storyboard.TargetNamerec1 To50 BeginTime0:0:0.0 Duration0:0:0.5 EasingFunction{StaticResource powerEase} AutoReverseTrue/DoubleAnimation Storyboard.TargetNamerec2 To50 BeginTime0:0:0.1 Duration0:0:0.5 EasingFunction{StaticResource powerEase} AutoReverseTrue/DoubleAnimation Storyboard.TargetNamerec3 To50 BeginTime0:0:0.2 Duration0:0:0.5 EasingFunction{StaticResource powerEase} AutoReverseTrue/DoubleAnimation Storyboard.TargetNamerec4 To50 BeginTime0:0:0.3 Duration0:0:0.5 EasingFunction{StaticResource powerEase} AutoReverseTrue/DoubleAnimation Storyboard.TargetNamerec5 To50 BeginTime0:0:0.4 Duration0:0:0.5 EasingFunction{StaticResource powerEase} AutoReverseTrue/DoubleAnimation Storyboard.TargetNamerec6 To50 BeginTime0:0:0.5 Duration0:0:0.5 EasingFunction{StaticResource powerEase} AutoReverseTrue/DoubleAnimation Storyboard.TargetNamerec7 To50 BeginTime0:0:0.6 Duration0:0:0.5 EasingFunction{StaticResource powerEase} AutoReverseTrue/DoubleAnimation Storyboard.TargetNamerec8 To50 BeginTime0:0:0.7 Duration0:0:0.5 EasingFunction{StaticResource powerEase} AutoReverseTrue/DoubleAnimation Storyboard.TargetNamerec9 To50 BeginTime0:0:0.8 Duration0:0:0.5 EasingFunction{StaticResource powerEase} AutoReverseTrue/DoubleAnimation Storyboard.TargetNamerec10 To50 BeginTime0:0:0.9 Duration0:0:0.5 EasingFunction{StaticResource powerEase} AutoReverseTrue//Storyboard/BeginStoryboard/EventTrigger/Grid.TriggersGrid.ColumnDefinitionsColumnDefinition Width15/ColumnDefinition Width15/ColumnDefinition Width15/ColumnDefinition Width15/ColumnDefinition Width15/ColumnDefinition Width15/ColumnDefinition Width15/ColumnDefinition Width15/ColumnDefinition Width15/ColumnDefinition Width15//Grid.ColumnDefinitionsGrid.RowDefinitionsRowDefinition/RowDefinition//Grid.RowDefinitionsLabel ContentLoading...Grid.Row1FontSize18FontFamilyTimes New RomanFontWeightBoldGrid.ColumnSpan10VerticalContentAlignmentCenterHorizontalContentAlignmentCenter/Rectangle Namerec1 Grid.Column0 Style{StaticResource rec}/Rectangle Namerec2 Grid.Column1 Style{StaticResource rec}/Rectangle Namerec3 Grid.Column2 Style{StaticResource rec}/Rectangle Namerec4 Grid.Column3 Style{StaticResource rec}/Rectangle Namerec5 Grid.Column4 Style{StaticResource rec}/Rectangle Namerec6 Grid.Column5 Style{StaticResource rec}/Rectangle Namerec7 Grid.Column6 Style{StaticResource rec}/Rectangle Namerec8 Grid.Column7 Style{StaticResource rec}/Rectangle Namerec9 Grid.Column8 Style{StaticResource rec}/Rectangle Namerec10 Grid.Column9 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()));}
}
一点拙见还请指正。各位大佬有更好的方法还请留个传送门或评论区展现一下风采!!!!
好记性不如烂笔头................................................................................................