开发者

WPF Splash Screen Windows

I need to have a PN开发者_JAVA百科G (with transparency) as a splash screen. The transparent portions of the image should be clear so the user can see any windows behind it (or desktop).

I also need to display the splash screen for 5 seconds (the contract specifically says 5 seconds) and it can't be any shorter. I am aware of the build property in VS 2010 but the splash screen comes and goes too quick (less than 5 seconds).

What can I do to make it stay 5 seconds (approximately)


I had a similar problem, where i couldn't use the built-in splashscreen option, on a WPF project.

That project is now open source, you have have a look here: https://code.google.com/p/theomniscientchimp/

It's an auto-updater (there are a few things you don't need i guess).

This is the minimum you should need:

WPF side:

<Window x:Class="TheOmniscientChimp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:CustomXaml"
        Icon="SC2_Replay_Monkey.ico"
        Title="MainWindow" Height="256" Width="456" Background="#00005555" AllowsTransparency="True" WindowStyle="None" WindowStartupLocation="CenterScreen" >
        <Grid Width="Auto">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />           
        </Grid.RowDefinitions>
        <Image Name="splashScreenImage" Stretch="Fill" Grid.Row="0" />
    </Grid>
</Window>

C# side (code behind):

/// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            BitmapImage splashScreenImageSource = new BitmapImage();
            splashScreenImageSource.BeginInit();
            splashScreenImageSource.UriSource = new Uri("Your_Image.png", UriKind.Relative);
            splashScreenImageSource.EndInit();

            splashScreenImage.Source = splashScreenImageSource;
        }

        public void AsynchronousExit()
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            worker.RunWorkerAsync();
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            //Makes the thread wait for 5s before exiting.
            Thread.Sleep(5000);
        }

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Environment.Exit(0);
        }
    }

Tell me if you need help to adjust.

FB.


If i had to do it i would add a window and set its properties AllowsTransparency = true; set it to start before all forms i mean before loading this can be done by modifying App.xml and set Startup="Application_Startup To disable the top defauld control you have to set WindowStyle = none and there in its code

private void Application_Startup(object sender, StartupEventArgs e)
        {
            MainWindow mn = new MainWindow();
            mn.ShowDialog();          
        }

use timer to do what ever you want

private DispatcherTimer timer;
timer = new DispatcherTimer();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = TimeSpan.FromMilliseconds(5000);
            timer.IsEnabled = true;

void timer_Tick(object sender, EventArgs e)
        {
///Close your window here
}

hope this helps


I had the same problem, buts it's actually suprisingly easy to solve without anything special:

  1. add your image in to the main project and set the build property to Splash Screen.

  2. add a Thread.Sleep(5000) into the constructor for your app's main window right before the InitializeComponents() call.

This will delay the loading of your main wpf window, and the splash screen will stay up for at least the load time + the time of the sleep before the main window pops up and the splash goes away.


You'll probably have to do something similar to what you'd do in WinForms. Spin up a new thread and start a Wpf Application on that thread. In WinForms you'd do this by using System.Windows.Forms.Application.Run(new SplashForm()); Should be something similar to this in Wpf.

EDIT: I found this, so it should work. http://msdn.microsoft.com/en-us/library/ms597011.aspx

The key is to do this ON A SEPARATE THREAD. The thread the application starts is is already tied to one WPF application and you want your splash to basically be its own GUI. The form can then have a timer that causes it to close itself, which should automatically terminate this other application.


If you use want a splash screen window and use the background worker, you make your main code more complex as it is not necessary. The solution is to write the main code as a normal synchronous code style in main UI thread while showing the splash window until you close it, and you can send an update to change the splash screen loading text or progress bar.

SplashWindow window = null;


var thread = new Thread(new ThreadStart(() =>
{
    Debug.Print("Thread begin");
    window = new SplashWindow();
    window.ShowDialog();
}));

thread.Name = "RunSplashWindow";
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();

Debug.Print("App begin");

Thread.Sleep(1000);

if (window != null)
{
    window.Dispatcher.BeginInvoke(new Del(() => {
        window.SetHeader("Running...");
    }), new object[0]);
}

Thread.Sleep(1000);

for (int i = 1; i <= 100; i++)
{
    if (window != null)
        {
             window.Dispatcher.BeginInvoke(new Del(() =>
             {
             window.SetProgress((double)i);
        }), new object[0]);
    }

    Thread.Sleep(10);
}

Thread.Sleep(1000);

Debug.Print("App end");

if (window != null)
{
    window.Dispatcher.BeginInvoke(new Del(() =>
    {
        window.Close();
    }), new object[0]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜