Textblock中的文本以动态更改

时间:2015-01-02 07:45:41

标签: c# windows-phone-8

HubSection Tag="2" Name="Stopwatch" x:Uid="Stopwatch" Header="STOPWATCH"

DataContext="{Binding Groups[1]}" HeaderTemplate="{ThemeResource HubSectionHeaderTemplate}">

<DataTemplate>

 <Grid>

<TextBox HorizontalAlignment="Center" Margin="10,36,11,350" TextWrapping="Wrap" Text="00:00:00" 


VerticalAlignment="Center" IsReadOnly="True" Width="320" Height="63" BorderThickness="0" 

Foreground="White" Background="Black" TextAlignment="Center" FontSize="42" CharacterSpacing="80" 

FontFamily="Segoe WP Semibold" Loaded="TextBox_Loaded"/>

</Grid>

</DataTemplate>

        </HubSection>

这是XAML PART

stopwatch_timer.Interval = new TimeSpan(0, 0, 0, 1, 0);
stopwatch_timer.Tick += Stopwatch_Tick;
TimerDisplay.Text = Start_Count++.ToString();

我的问题是如何增加文本块的零?

因为当我运行这个时,所有零点消失并且它从单个零点开始增加。

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,这可能会对你有所帮助:

private TimeSpan time;
private void Timer()
{
     DispatcherTimer stopwatch_timer = new DispatcherTimer();
     stopwatch_timer.Interval = new TimeSpan(0, 0, 0, 1, 0);
     stopwatch_timer.Tick += new EventHandler(stopwatch_timer_Tick);
     stopwatch_timer.Start();
     time = stopwatch_timer.Interval;
}        
void stopwatch_timer_Tick(object sender, EventArgs e)
{
     TimerDisplay.Text = time.ToString();
     time = time.Add(((DispatcherTimer)sender).Interval);   
}

答案 1 :(得分:0)

您需要创建一个Timer tick事件处理程序来显示时间。

// creating timer instance
DispatcherTimer newTimer = new DispatcherTimer();
// timer interval specified as 1 second
newTimer.Interval = TimeSpan.FromSeconds(1);
// Sub-routine OnTimerTick will be called at every 1 second
newTimer.Tick += OnTimerTick;
// starting the timer
newTimer.Start();

以下是OnTimerTick

的代码
void OnTimerTick(Object sender, EventArgs args)
{
        // text box property is set to current system date.
        // ToString() converts the datetime value into text
        TimerDisplay.Text = DateTime.Now.ToString();
}

浏览此链接http://developer.nokia.com/community/wiki/Implement_timers_in_Windows_Phone

相关问题