1

第一的:

public MainPage()
{
    InitializeComponent();

    this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    Verdienen v = new Verdienen(4, 3);
}

然后: public Verdienen(int attesaInSecondiIniziale = 20, int attesaInSecondiFinale = 8) { this.AttesaInSecondiIniziale = attesaInSecondiIniziale; this.AttesaInSecondiIniziale = attesaInSecondiFinale; MostraPerQuestaSezione = false;

        popup = new Popup();
        Border border = new Border();
        border.Background = new SolidColorBrush(Colors.LightGray);
        border.Margin = new Thickness(3);
        StackPanel panelVerticale = new StackPanel();
        panelVerticale.Orientation = Orientation.Vertical;
        AdControl control = new AdControl();
        panelVerticale.Children.Add(control);
        StackPanel panelOrizzontale = new StackPanel();
        panelOrizzontale.Orientation = Orientation.Horizontal;
        Button bAltreApp = new Button();
        bAltreApp.Content = "";
        bAltreApp.Tap += new EventHandler<GestureEventArgs>(bAltreApp_Tap);
        Button bVota = new Button();
        bVota.Tap += new EventHandler<GestureEventArgs>(bVota_Tap);
        bVota.Content = "";
        panelOrizzontale.Children.Add(bAltreApp);
        panelOrizzontale.Children.Add(bVota);
        panelVerticale.Children.Add(panelOrizzontale);
        border.Child = panelVerticale;
        popup.Child = border;

        this.ShowPopup();
    }

    private async **System.Threading.Tasks.TaskEx** ShowPopup()
    {
        do
        {
            Debug.WriteLine("thread iniziato. pausa cominciata");
            await System.Threading.Tasks.TaskEx.Delay(1000 *    this.AttesaInSecondiIniziale);
            Debug.WriteLine("thread: fine pausa");
            popup.IsOpen = true;
            await System.Threading.Tasks.TaskEx.Delay(1000 * this.AttesaInSecondiFinale);
            popup.IsOpen = false;
        } while (MostraPerQuestaSezione);
    }

你能告诉我为什么这段代码不显示弹出窗口吗?注意:一些不必要的代码不存在!编辑:请注意System.Threading.Tasks.TaskEx被标记为错误(“异步方法的返回状态必须为 void、Task 或 Task”)。

4

2 回答 2

0

使用 时Thread.Sleep,会阻止 UI 消息泵。这会阻止系统显示您的弹出窗口,因为它在正确处理消息之前不会显示。

更好的方法是将其移动到异步方法中,并在构造后调用它:

public async Task ShowPopup(int attesaInSecondiIniziale = 20, int attesaInSecondiFinale = 8)
{
   // Your code...

   do
   {
      await Task.Delay(1000 * this.AttesaInSecondiIniziale);
      this.ShowPopup();
      await Task.Delay(1000 * this.AttesaInSecondiIniziale);
      this.HidePopup();       
   }
   while (MostraPerQuestaSezione);
} 

通过在异步方法中使用Task.Delaywith await,您不会阻塞 UI。但是,这需要使用异步定位包来定位 WP7.5 。

于 2013-03-27T16:14:32.457 回答
0
this.ShowPopup(); ...
this.HidePopup();

应该

popup.ShowPopup(); ...
popup.HidePopup();

你正在打电话this,在这种情况下,this是一个Verienen对象,你没有

ShowPopup()HidePopup()方法

于 2013-03-27T16:14:44.147 回答