每5秒重新加载一次silverlight页面

时间:2011-09-12 07:49:12

标签: c# silverlight

我在silverlight中创建了一个包含按钮的页面,通过单击此按钮,它将启动一个计时器,每个计时器勾选它在页面中创建一个矩形,每个矩形紧挨着另一个,直到页面满了矩形

我的问题是如何在完整的矩形页面重新加载页面?

聚苯乙烯。我创建了代码(.cs)NOT .xaml的页面,我还想让它重新加载Silverlight代码(.cs)NOT .xaml

2 个答案:

答案 0 :(得分:1)

首先,您不想重新加载页面(在传统意义上),因为这将重新启动您的silverlight应用程序。

你看过WriteableBitmapEx(http://writeablebitmapex.codeplex.com/)吗?您可以使用它来绘制矩形,然后清除屏幕。

如果这没有帮助,请告知您如何绘制矩形。

答案 1 :(得分:0)

查看System.Threading.Timer class。它允许您按间隔安排任务。重新加载整个页面的示例(您应该考虑仅清除Pino建议的页面):

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线程中。如果你不这样做,你会得到一个例外。