【问题标题】:Text in Textblock to change Dynamically文本块中的文本动态更改
【发布时间】:2015-01-03 00:30:26
【问题描述】:
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 部分

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

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

因为当我运行它时,所有零都消失了,它从单个零开始递增。

【问题讨论】:

  • 虽然我不确定你想在 C# 代码中做什么,但是如果你设置 TimerDisplay.Text = stopwatch_timer.Interval 呢?尝试根据需要显示 TimeSpan 值本身,以便保留时间格式。

标签: c# windows-phone-8


【解决方案1】:

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

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);   
}

【讨论】:

    【解决方案2】:

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

    // 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      相关资源
      最近更新 更多