C#暂停线程暂停整个应用程序

时间:2014-03-10 14:52:28

标签: c# xaml

我只想制作一个带加载文字的小屏幕。但我得到的是一个白色的屏幕,在代码完成时启动,为什么会这样?

代码:

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>

3 个答案:

答案 0 :(得分:1)

这是因为你正在主线程上进行休眠。我建议启动一个处理启动画面的单独(工作者)线程。

答案 1 :(得分:0)

使用DispatcherTimer对象,它是UI线程识别并且很简单。

Comparing Timer with DispatcherTimer

答案 2 :(得分: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();