2011-09-12 78 views
0

我在silverlight中創建了一個頁面,其中包含一個按鈕,通過單擊此按鈕它將啓動一個計時器,每個計時器勾選它在頁面中創建一個矩形,每個矩形與另一個直到頁面滿了矩形。使silverlight頁面每5秒刷新一次

我的問題是如何使頁面重新加載時,其充滿矩形?

Ps。我使用代碼(.cs)創建了不是.xaml的頁面,並且我還希望使其在Silverlight代碼(.cs)中重新加載而不是.xaml

回答

1

首先,您不想重新加載頁面(在傳統意義上),因爲這將重啓你的Silverlight應用程序。

你是否看了WriteableBitmapEx(http://writeablebitmapex.codeplex.com/)?您可以使用它來繪製矩形,然後清除屏幕。

如果這沒有幫助,請告知你如何繪製你的方形。

+0

我創建的矩形編程,使用代碼不XAML,現在我想重新加載頁面時,它完全用矩形框(空從矩形的頁面,點擊按鈕重新啓動),我想用代碼來做到這一點 – zaidmctaie

0

看看System.Threading.Timer class。它允許您間隔安排任務。從而重新加載整個頁面的一個例子(你應該只考慮到清除頁面皮諾建議):

public partial class MainPage : UserControl 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 
     timer = new Timer(TimerElapsed); 
    } 

    // hold the timer in a variable to prevent it from being garbage collected 
    private Timer timer = null; 

    private void TimerElapsed(object state) 
    { 
     // important: this line puts the timer call into UI thread 
     Dispatcher.BeginInvoke(() => { 
      // your code goes here... 
      // reload the page (this will reload the app and stop the timer!) 
      HtmlPage.Window.Eval("location.reload()"); 
     }); 
    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     // start the timer in 1 second and repeat every 5 seconds 
     timer.Change(1000, 5000); 
    } 
} 

關注在代碼中的註釋。有必要將Timer存儲在類範圍的變量中。否則它不會被引用並且可能被垃圾收集。如果你想改變頁面上的東西在定時操作,則必須使用Dispatcher對象放入UI線程。如果你不這樣做,你會得到一個例外。