2013-03-27 168 views
1

第一:爲什麼Popup沒有顯示?

public MainPage() 
{ 
    InitializeComponent(); 

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

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

然後: 公共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被標記爲錯誤(「異步方法的返回狀態必須爲無效,任務或任務」)。

+0

凡ShowPopup的代碼? – zzfima 2013-03-27 16:13:57

+0

我已經升級了代碼。請再看一遍! – Spode 2013-03-28 10:53:54

回答

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,你會不會阻止用戶界面。不過,這需要將WP7.5定位爲async targeting pack

+0

好的......但是如何添加System.Threading.Tasks?我只能添加System.Threading ... 我在我的項目中使用了「PM> Install-Package System.Threading.Tasks -Pre」,但它沒有被添加... – Spode 2013-03-27 20:17:32

+0

ok!只需重新加載SDK! – Spode 2013-03-27 20:25:25

+0

對不起,但它似乎等待Task.Delay(int)將只啓動WP8 – Spode 2013-03-27 20:30:49

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

應該

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

要調用this在這方面,thisVerienen對象,你有沒有

ShowPopup()HidePopup()方法

+0

不......我不相信它可能是解決方案,因爲我聲明Popup popup是全局變量。謝謝! =) – Spode 2013-03-27 20:18:15

+0

那麼這是什麼?如果它沒有引用'Verdienen()'的對象,那麼我對C#和OOP的理解就離開了。闡述! – sircapsalot 2013-03-27 20:22:15

+0

是的,這是指Verdienen的例子。但是在這個類中,我聲明Popup popup是全局對象;) – Spode 2013-03-27 20:32:34