正在为 Windows 商店应用更新计时器中的分钟数
Updating minutes in a Timer for Windows store app
我正在尝试为我的 windows 商店应用程序创建一个计时器,
我已经能够更新秒数,但不是分钟,
分钟从组合框中获取其值
我哪里做错了?
public sealed partial class MainPage : Page
{
int secCount = 0;
DispatcherTimer timerCount = new DispatcherTimer();
TextBlock time = new TextBlock();
int m;
public MainPage()
{
this.InitializeComponent();
timerCount.Interval = new TimeSpan(0, 0, 0, 1, 0);
timerCount.Tick += new EventHandler<object>(timer_tick);
m = Convert.ToInt32(cMinutes.SelectedValue);
}
private void timer_tick(object sender, object e)
{
time.Text = cHours.SelectedItem + ":" + m + ":" + secCount;
timer.Children.Add(time); //timer is the main grid
secCount--;
if(secCount < 0)
{
secCount = 59;
m--;
}
}
首先,您的代码存在问题,您将 TextBlock time
重复添加到 timer
网格,您应该得到一个异常,抱怨 TextBlock 已经是网格的一个children.
问题的答案
您需要更新 cMinutes 的 SelectedValue 或 SelectedIndex。
一些改进:
我将字段 timerCount
和 time
的初始化移动到 MainPage 的构造函数中,在一个地方初始化所有字段是一个很好的做法。
由于 int (secCount
和 m
) 默认为 0,因此您不需要为 int 设置初始值。
public sealed partial class MainPage : Page
{
int secCount;
int m;
DispatcherTimer timerCount; //declare here, initialize in constructor
TextBlock time; //declare here, initialize in constructor
public MainPage()
{
this.InitializeComponent();
time = new TextBlock();
timer.Children.Add(time); //fix: only add ONCE
//populate the Combobox
cMinutes.Items.Add(0);
cMinutes.Items.Add(1);
cMinutes.Items.Add(2);
cMinutes.SelectedIndex = 2;
m = Convert.ToInt32(cMinutes.SelectedValue);
timerCount = new DispatcherTimer();
timerCount.Interval = new TimeSpan(0, 0, 0, 1, 0);
timerCount.Tick += new EventHandler<object>(timer_tick);
timerCount.Start();
}
private void timer_tick(object sender, object e)
{
time.Text = cHours.SelectedItem + ":" + m + ":" + secCount;
secCount--;
if (secCount < 0)
{
secCount = 59;
m--;
if (cMinutes.Items.Contains(m)) //fix: update Combobox's selected value
cMinutes.SelectedValue = m;
}
}
}
我正在尝试为我的 windows 商店应用程序创建一个计时器, 我已经能够更新秒数,但不是分钟, 分钟从组合框中获取其值
我哪里做错了?
public sealed partial class MainPage : Page
{
int secCount = 0;
DispatcherTimer timerCount = new DispatcherTimer();
TextBlock time = new TextBlock();
int m;
public MainPage()
{
this.InitializeComponent();
timerCount.Interval = new TimeSpan(0, 0, 0, 1, 0);
timerCount.Tick += new EventHandler<object>(timer_tick);
m = Convert.ToInt32(cMinutes.SelectedValue);
}
private void timer_tick(object sender, object e)
{
time.Text = cHours.SelectedItem + ":" + m + ":" + secCount;
timer.Children.Add(time); //timer is the main grid
secCount--;
if(secCount < 0)
{
secCount = 59;
m--;
}
}
首先,您的代码存在问题,您将 TextBlock time
重复添加到 timer
网格,您应该得到一个异常,抱怨 TextBlock 已经是网格的一个children.
问题的答案
您需要更新 cMinutes 的 SelectedValue 或 SelectedIndex。
一些改进:
我将字段 timerCount
和 time
的初始化移动到 MainPage 的构造函数中,在一个地方初始化所有字段是一个很好的做法。
由于 int (secCount
和 m
) 默认为 0,因此您不需要为 int 设置初始值。
public sealed partial class MainPage : Page
{
int secCount;
int m;
DispatcherTimer timerCount; //declare here, initialize in constructor
TextBlock time; //declare here, initialize in constructor
public MainPage()
{
this.InitializeComponent();
time = new TextBlock();
timer.Children.Add(time); //fix: only add ONCE
//populate the Combobox
cMinutes.Items.Add(0);
cMinutes.Items.Add(1);
cMinutes.Items.Add(2);
cMinutes.SelectedIndex = 2;
m = Convert.ToInt32(cMinutes.SelectedValue);
timerCount = new DispatcherTimer();
timerCount.Interval = new TimeSpan(0, 0, 0, 1, 0);
timerCount.Tick += new EventHandler<object>(timer_tick);
timerCount.Start();
}
private void timer_tick(object sender, object e)
{
time.Text = cHours.SelectedItem + ":" + m + ":" + secCount;
secCount--;
if (secCount < 0)
{
secCount = 59;
m--;
if (cMinutes.Items.Contains(m)) //fix: update Combobox's selected value
cMinutes.SelectedValue = m;
}
}
}