0

I just wish to make a little splash screen with loading text. But what I get is a white screen that launches when the code finishes, why is that?

Code:

public partial class SplashScreen : Page
{
    public SplashScreen()
    {
        InitializeComponent();
    }

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        Loading.Content = "Loading";
        Thread.Sleep(500);
        Loading.Content = "Loading.";
        Thread.Sleep(500);
        Loading.Content = "Loading..";
        Thread.Sleep(500);
        Loading.Content = "Loading...";
        Thread.Sleep(500);

//when gets to here the page can be seen, not with loading... "animation:
    }
}

XAML:

<Viewbox>
    <Grid>
        <Image x:Name="Overview_Picture" Source="/WPF_Unity;component/Images/Splash.jpg" />
        <Label HorizontalAlignment="Center" x:Name="Loading" FontSize="54" Content="Loading..." Foreground="#a09c9d" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Bottom" FontFamily="pack://application:,,,/Fonts/#Univers LT Std 57 Cn" FontWeight="Bold" Margin="0,0,0,400" /> 
    </Grid>
</Viewbox>

jQuerybeast
  • 14,130
  • 38
  • 118
  • 196
Dim
  • 4,527
  • 15
  • 80
  • 139
  • 1
    It seems like you need to read (and understand) the [basics of threading](http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx) before trying to do this. – Federico Berasategui Mar 10 '14 at 14:54
  • 2
    You are blocking your main UI thread. Replace `Thread.Sleep(500);` with `await Task.Delay(500);` – L.B Mar 10 '14 at 14:55
  • 1
    You could try using `async` here... change your function to `private async void Page_Loaded` and then change `Thread.Sleep` to `await Task.Delay(500)`. I'm not sure enough this will work in your case to post it as an answer, but I think it will. – John Gibb Mar 10 '14 at 14:56
  • Is this the splash screen for your application while starting or a progress you like to show (loading content) while the app is already running? A Page is not intended to run independently. It needs to be hosted. How do you start your splash screen (who owns the thread)? – BionicCode Mar 10 '14 at 15:16

3 Answers3

1

This is because you are doing the sleeps on the main thread. I would suggest to start a seperate (worker-)thread that is handling your splashscreen.

RvdV79
  • 2,002
  • 16
  • 36
0

Use a DispatcherTimer object, it is UI thread aware and is simple.

Comparing Timer with DispatcherTimer

Community
  • 1
  • 1
T McKeown
  • 12,971
  • 1
  • 25
  • 32
0
new Thread(() =>
        {
            Thread.CurrentThread.IsBackground = true;


                this.Dispatcher.Invoke((Action)(() =>
                {
                    Loading.Content = "Loading";
                }));

            Thread.Sleep(500);
            this.Dispatcher.Invoke((Action)(() =>
            {
                Loading.Content = "Loading.";
            }));
            Thread.Sleep(500);
            this.Dispatcher.Invoke((Action)(() =>
            {
                Loading.Content = "Loading..";
            }));
            Thread.Sleep(500);
            this.Dispatcher.Invoke((Action)(() =>
            {
                Loading.Content = "Loading...";
            }));
            Thread.Sleep(500);


        }).Start();
Dim
  • 4,527
  • 15
  • 80
  • 139