本文介绍了Metro应用程序的C#中倒数计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好,我需要构建一个使用倒计时(年,日,小时,分钟,秒)的应用。我已经尝试编写代码并且我成功计算了时间,但是当我尝试将此时间作为字符串存储在texblock中时,他没有更新。任何人都可以帮我设置文本块来更新自己吗? 注意:我是C#和Metro App的初学者。抱歉我的英语不好。 使用系统; 使用 System.Collections.Generic; 使用 System.IO; 使用 System.Linq; 使用 Windows.Foundation; 使用 Windows.Foundation.Collections; 使用 Windows.UI.Xaml; 使用 Windows.UI.Xaml.Controls; 使用 Windows.UI.Xaml.Controls.Primitives; 使用 Windows.UI.Xaml.Data; 使用 Windows.UI.Xaml.Input; 使用 Windows.UI.Xaml.Media; 使用 Windows.UI.Xaml.Navigation; 命名空间 Christmas_Countdown { public 密封 部分 类 MainPage:Page { public MainPage() { this .InitializeComponent(); } 受保护 覆盖 void OnNavigatedTo(NavigationEventArgs e) {} private void Button_Click_1( object sender,RoutedEventArgs e) { DispatcherTimer t = new DispatcherTimer(); DateTime endtime = new DateTime( 2012 , 12 , 25 , 0 , 0 , 0 ); DateTime now = DateTime.Now; t.Interval = endtime - now; TimeSpan ts = t.Interval; t.Start(); string countDown = string .Format( {0}天,{1}小时,{2}分钟,{3}秒到圣诞节。,t。Interval。 Days,t.Interval.Hours,t.Interval.Minutes,t.Interval.Seconds); Day_TxtBlk.Text = countDown; } private void Day_TxtBlk_Loaded( object sender,RoutedEventArgs e) { } } } 解决方案 我找到了解决问题的方法。 void t_Tick( object sender,对象 e) { TimeSpan ts = endtime.Subtract(DateTime.Now); string countdowndays = ts.Days.ToString(); daytxtblk.Text = countdowndays; string countdownhours = ts.Hours.ToString(); hourstxtblk.Text = countdownhours; string countdownminutes = ts.Minutes.ToString(); minutetxtblk.Text = countdownminutes; string countdownseconds = ts.Seconds.ToString(); sectxtblk.Text = countdownseconds; } private void christmas_2012_Click( object sender,RoutedEventArgs e) { if ( this .IsEnabled) { txtboxchr.Visibility = Visibility.Visible; txtboxhours.Visibility = Visibility.Visible; txtboxminutes.Visibility = Visibility.Visible; txtboxseconds.Visibility = Visibility.Visible; daytxtblk.Visibility = Visibility.Visible; hourstxtblk.Visibility = Visibility.Visible; minutetxtblk.Visibility = Visibility.Visible; sectxtblk.Visibility = Visibility.Visible; } t = new DispatcherTimer(); endtime = new DateTime( 2012 , 12 , 25 , 0 , 0 , 0 ); if (DateTime.Now.Year > endtime.Year) { t.Stop(); txtboxchr.Visibility = Visibility.Collapsed; txtboxhours.Visibility = Visibility.Collapsed; txtboxminutes.Visibility = Visibility.Collapsed; txtboxseconds.Visibility = Visibility.Collapsed; daytxtblk.Visibility = Visibility.Collapsed; hourstxtblk.Visibility = Visibility.Collapsed; minutetxtblk.Visibility = Visibility.Collapsed; sectxtblk.Visibility = Visibility.Collapsed; pleasesetdat(); } 其他 { t.Interval = new TimeSpan( 1000 ); TimeSpan ts = endtime.Subtract(DateTime.Now); t.Tick + = t_Tick; t.Start(); } } Hello, i need to build an app that use a countdown (Years, Days, Hour, Minutes, Seconds). I''ve tried to write a code and i succeed to calculate the time, but when i try to put this time as a string in a texblock, he is not updating. Can anyone help me to set the textblock to update himself ? Notice: I''m a beginner in C# and Metro App''s. And sorry for my bad english.using System;using System.Collections.Generic;using System.IO;using System.Linq;using Windows.Foundation;using Windows.Foundation.Collections;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Controls.Primitives;using Windows.UI.Xaml.Data;using Windows.UI.Xaml.Input;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Navigation;namespace Christmas_Countdown{ public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { } private void Button_Click_1(object sender, RoutedEventArgs e) { DispatcherTimer t = new DispatcherTimer(); DateTime endtime = new DateTime(2012, 12, 25, 0, 0, 0); DateTime now = DateTime.Now; t.Interval = endtime - now; TimeSpan ts = t.Interval; t.Start(); string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds til Christmas.", t.Interval.Days, t.Interval.Hours, t.Interval.Minutes, t.Interval.Seconds); Day_TxtBlk.Text = countDown; } private void Day_TxtBlk_Loaded(object sender, RoutedEventArgs e) { } }} 解决方案 I found the solution to my problem.void t_Tick(object sender, object e) { TimeSpan ts = endtime.Subtract(DateTime.Now); string countdowndays = ts.Days.ToString(); daytxtblk.Text = countdowndays; string countdownhours = ts.Hours.ToString(); hourstxtblk.Text = countdownhours; string countdownminutes = ts.Minutes.ToString(); minutetxtblk.Text = countdownminutes; string countdownseconds = ts.Seconds.ToString(); sectxtblk.Text = countdownseconds; } private void christmas_2012_Click(object sender, RoutedEventArgs e) { if (this.IsEnabled) { txtboxchr.Visibility = Visibility.Visible; txtboxhours.Visibility = Visibility.Visible; txtboxminutes.Visibility = Visibility.Visible; txtboxseconds.Visibility = Visibility.Visible; daytxtblk.Visibility = Visibility.Visible; hourstxtblk.Visibility = Visibility.Visible; minutetxtblk.Visibility = Visibility.Visible; sectxtblk.Visibility = Visibility.Visible; } t = new DispatcherTimer(); endtime = new DateTime(2012, 12, 25, 0, 0, 0); if (DateTime.Now.Year > endtime.Year) { t.Stop(); txtboxchr.Visibility = Visibility.Collapsed; txtboxhours.Visibility = Visibility.Collapsed; txtboxminutes.Visibility = Visibility.Collapsed; txtboxseconds.Visibility = Visibility.Collapsed; daytxtblk.Visibility = Visibility.Collapsed; hourstxtblk.Visibility = Visibility.Collapsed; minutetxtblk.Visibility = Visibility.Collapsed; sectxtblk.Visibility = Visibility.Collapsed; pleasesetdat(); } else { t.Interval = new TimeSpan(1000); TimeSpan ts = endtime.Subtract(DateTime.Now); t.Tick += t_Tick; t.Start(); } } 这篇关于Metro应用程序的C#中倒数计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-19 01:26