简体   繁体   English

任务延迟不等待(Windows Phone 8应用程序)

[英]Task delay not waiting (windows phone 8 application)

I'm trying to perform a really simple Task. 我正在尝试执行一个非常简单的任务。 I've started a new project using VS Express 2013, choosing a Windows Phone 8 application (instead of Silverlight). 我使用VS Express 2013开始了一个新项目,选择了Windows Phone 8应用程序(而不是Silverlight)。 On xaml side, the interface contains one button and one rectangle. 在xaml侧,界面包含一个按钮和一个矩形。 What I want to do is : when the button is clicked, the rectangle will be filled with a green background then after 1 second, the background will shift to red. 我想要做的是:当点击按钮时,矩形将填充绿色背景,然后在1秒后,背景将变为红色。 Finally the text in the button is changed to "FINISHED", simply to check that everything behaved in the correct order. 最后,按钮中的文本更改为“FINISHED”,只是为了检查所有内容的行为是否正确。 At first, I tried to use Thread.Sleep methods, but this "works" only with Silverlight if I understand well. 起初,我尝试使用Thread.Sleep方法,但如果我理解的话,这只适用于Silverlight。 So I've searched and found many different posts and I am a bit confused between Tasks, threads, TPL, etc.. Here is what I tried (located in my main) 所以我搜索并找到了许多不同的帖子,我在任务,线程,TPL等之间有点混淆。这是我尝试的(位于我的主要)

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();           
        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        // TODO: préparer la page pour affichage ici.

    }
private void BtnClick(object sender, RoutedEventArgs e)
    {
        //Task task = Task.Factory.StartNew( () => DoWait());
        DoWait();
    }

    public async void DoWait()
    {
        SolidColorBrush greenBrush = new SolidColorBrush(Colors.Green);
        SolidColorBrush redBrush = new SolidColorBrush(Colors.Red);
        BtnGreen();
        await Task.Delay(1000);
        Rect1.Fill = redBrush;
        await Task.Delay(1000);               
        LabelChange();
    }

    public void LabelChange()
    {
        Btn.Content = "FINISHED";
    }

    public void BtnGreen()
    {
        SolidColorBrush greenBrush = new SolidColorBrush(Colors.Green);
        Rect1.Fill = greenBrush;
    }

However the following happens : - The rectangle is filled with red color - There is a 1 second pause - The text in the button is changed. 但是会发生以下情况: - 矩形填充红色 - 暂停1秒 - 按钮中的文本已更改。 So, everything goes fine except the first pause (between the rectangle going green, and then red). 所以,除了第一次暂停(在矩形变为绿色,然后是红色之间)之外,一切都很顺利。 It is as if the first "await Task.delay(1000)" is not waited after (ie I never see the rectangle changing to green). 就好像第一个“等待Task.delay(1000)”之后没有等待(即我从未看到矩形变为绿色)。

I've tried to find an explanation but I'm totally lost... If someone could help, I'd be most grateful. 我试图找到一个解释,但我完全迷失了......如果有人可以提供帮助,我将非常感激。

EDIT : here is a more detailed description of the problem The first time I click on the button, there is a short delay before the rectangle goes green, but when I further click on the button, the rectangle will shift to green instantaneously. 编辑:这里是问题的更详细描述第一次点击按钮,矩形变为绿色之前有一个短暂的延迟,但是当我进一步点击按钮时,矩形将立即变为绿色。 So the delay (UI blocked ?) happens only the first time the button is clicked 所以延迟(UI阻止?)仅在第一次单击按钮时发生

Following Romasz answer, I found out my problem occured only when running the app from the emulator. 按照Romasz的回答,我发现只有在从模拟器运行应用程序时才会出现问题。 When relased on the phone, everything works fine. 在手机上接通时,一切正常。 So it was probably a problem of loading dynamic libraries. 所以这可能是加载动态库的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM