欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF 启动页面

程序员文章站 2022-07-13 22:23:40
...

如果我写的有误,请及时与我联系,我立即改之以免继续误导他/她人。

如果您有好的想法或者建议,请随时与我联系。


wpf软件启动时,加载启动页面。软件初始化完成之后关闭页面。


App.xaml.cs代码 (实现加载页面功能)

public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            var thread = new Thread(new ThreadStart(() =>
            {
                WpfApplication1.MainWindow.window = new Window1();
                WpfApplication1.MainWindow.window.Show();
                WpfApplication1.MainWindow.window.Activate();
                System.Windows.Threading.Dispatcher.Run();
            }));
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Start();
            base.OnStartup(e);
        }
    }

MainWindow.cs

public partial class MainWindow : Window
    {
        public static Window1 window = null;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Thread.Sleep(1000*10);

            if (window != null)
            {
                window.Dispatcher.BeginInvoke((Action) (() =>
                {
                    window.Close();
                }));
            }
        }
    }


启动页面

<Window x:Class="WpfApplication1.Window1"
        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"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d" WindowStartupLocation="CenterScreen" 
        Title="Window1" Height="300" Width="300" WindowStyle="None" MouseLeftButtonDown="Window_MouseLeftButtonDown">
    <Grid>
        <Image Name="splashScreenImage" Stretch="Fill" Grid.Row="0" Source="pp.jpg" />
        <Canvas Height="120" HorizontalAlignment="Center" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Center" Width="120">
            <Canvas.RenderTransform>
                <RotateTransform x:Name="SpinnerRotate" Angle="0" />
            </Canvas.RenderTransform>
            <Canvas.Triggers>
                <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                    <BeginStoryboard>
                        <BeginStoryboard.Storyboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:2" From="0" RepeatBehavior="Forever" Storyboard.TargetName="SpinnerRotate" Storyboard.TargetProperty="(RotateTransform.Angle)" To="360" />
                            </Storyboard>
                        </BeginStoryboard.Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Canvas.Triggers>
            <Ellipse Canvas.Left="47.2783" Canvas.Top="0.5" Fill="Orange" Height="21.862" Opacity="1" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="20.1696" Canvas.Top="9.76358" Fill="Orange" Height="21.862" Opacity="0.9" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="2.86816" Canvas.Top="29.9581" Fill="Orange" Height="21.862" Opacity="0.8" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="5.03758e-006" Canvas.Top="57.9341" Fill="Orange" Height="21.862" Opacity="0.7" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="12.1203" Canvas.Top="83.3163" Fill="Orange" Height="21.862" Opacity="0.6" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="36.5459" Canvas.Top="98.138" Fill="Orange" Height="21.862" Opacity="0.5" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="64.6723" Canvas.Top="96.8411" Fill="Orange" Height="21.862" Opacity="0.4" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="87.6176" Canvas.Top="81.2783" Fill="Orange" Height="21.862" Opacity="0.3" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="98.165" Canvas.Top="54.414" Fill="Orange" Height="21.862" Opacity="0.2" Stretch="Fill" Width="21.835" />
            <Ellipse Canvas.Left="92.9838" Canvas.Top="26.9938" Fill="Orange" Height="21.862" Opacity="0.1" Stretch="Fill" Width="21.835" />
        </Canvas>
    </Grid>
</Window>
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            base.DragMove();
        }

完整代码

点击打开链接




相关标签: wpf 启动页面