为什么Popup没有显示?

时间:2013-03-27 16:11:30

标签: c# windows-phone-7.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 被标记为错误(“异步方法的返回状态必须为空,任务或任务”)。

2 个答案:

答案 0 :(得分: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.Delayawait,您不会阻止UI。但是,这需要使用async targeting pack定位WP7.5。

答案 1 :(得分:0)

this.ShowPopup(); ...
this.HidePopup();

应该是

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

您正在呼叫this,在此上下文中,thisVerienen对象而您没有

ShowPopup()HidePopup()方法

相关问题