C# Windows Phone 7永久更新文本块

C# Windows Phone 7永久更新文本块,c#,windows-phone-7,stopwatch,C#,Windows Phone 7,Stopwatch,我正在尝试为WindowsPhone7编程某种秒表。为了测量经过的时间,我使用Stopwatch类。要打印输出,我使用文本块。但是我希望文本块始终显示经过的时间 Unitl现在我只能在事件上更新文本块(我使用按钮\点击事件) 我尝试了一段时间(真的)循环,但这只会冻结手机 有人知道如何解决这个问题吗?StopWatch类没有任何事件,因此如果要绑定,必须编写自己的类或使用计时器轮询StopWatch。 可以使用绑定将属性从TextBlock绑定到秒表。首先将此DataContext绑定添加到页面

我正在尝试为WindowsPhone7编程某种秒表。为了测量经过的时间,我使用Stopwatch类。要打印输出,我使用文本块。但是我希望文本块始终显示经过的时间

Unitl现在我只能在事件上更新文本块(我使用按钮\点击事件) 我尝试了一段时间(真的)循环,但这只会冻结手机


有人知道如何解决这个问题吗?

StopWatch类没有任何事件,因此如果要绑定,必须编写自己的类或使用计时器轮询StopWatch。 可以使用绑定将属性从TextBlock绑定到秒表。首先将此DataContext绑定添加到页面xaml

 <phone:PhoneApplicationPage
      DataContext="{Binding RelativeSource={RelativeSource Self}}" >
还有某个地方的计时器代码

        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(0.2); // customize update interval
        timer.Tick += delegate(object sender, EventArgs e)
        {
            StopwatchTime = sw.Elapsed.Seconds.ToString(); // customize format
        };
        timer.Start();

谢谢你的快速回答。我在找类似装订的东西,但没有找到。实际上,它不起作用(我认为应该如此),我想使用变量字符串show=randomTimespanvariable.ToString();绑定不起作用的原因很简单。秒表。已用不是一个独立属性。之所以不是,是因为您不想每1毫秒更新一次它的值。您的案例没有优雅的绑定解决方案。我尝试了上面的代码,即使以
Timespan.FromSeconds(0.1)
作为间隔,它也非常有效。如果仍然需要绑定,只需将myStopWatch.Appeased值分配到字符串类型的DependencyPerty中,然后将TextBlock绑定到该属性,但从功能上讲,您不会更改任何内容,也许不需要调用调度程序。如果需要代码帮助,请告诉我……我想使用变量字符串show=randomTimespanvariable.ToString();但“文本块”保持无效:(我想我对变量的范围有一些问题。当变量show在主页中定义时,我必须写:WindowsPhoneApplication1.MainPage.show我必须这样写吗?很抱歉,我做得太糟了。我总是想按enter键换行,它会在我完成之前发送评论。是的,我会很高兴的。)提供一些帮助。您是否考虑过类似的问题?
TimeSpan elapsedTime=stopWatch.appeased;String textBoxText=elapsedTime.ToString();
然后像这样绑定它?
    public static readonly DependencyProperty StopwatchTimeProperty =
        DependencyProperty.Register("StopwatchTime", typeof(string), typeof(MainPage), new PropertyMetadata(string.Empty));

    public string StopwatchTime
    {
        get { return (string)GetValue(StopwatchTimeProperty); }
        set { SetValue(StopwatchTimeProperty, value); }
    }
        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(0.2); // customize update interval
        timer.Tick += delegate(object sender, EventArgs e)
        {
            StopwatchTime = sw.Elapsed.Seconds.ToString(); // customize format
        };
        timer.Start();