【问题标题】:Updating minutes in a Timer for Windows store app更新 Timer for Windows 商店应用程序中的分钟数
【发布时间】:2015-06-19 08:03:55
【问题描述】:

我正在尝试为我的 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--;
        }
    }

【问题讨论】:

    标签: c# windows visual-studio windows-phone-8 windows-store-apps


    【解决方案1】:

    首先,您的代码中存在问题,您将 TextBlock time 反复添加到 timer Grid 中,并且您应该得到一个异常,抱怨 TextBlock 已经是 Grid 的孩子之一。

    问题的答案

    您需要更新 cMinutes 的 SelectedValue 或 SelectedIndex。

    一些增强功能:

    我将字段timerCounttime 的初始化移动到MainPage 的构造函数中,在一个地方初始化所有字段是一个好习惯。

    由于 int(secCountm)默认为 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;
            }
        }
    }
    

    【讨论】:

    • 没有用,它总是从组合框中检索第三个项目,我也没有得到你添加的那些行 if (cMinutes.Items.Contains(m)) cMinutes.SelectedValue =米;
    • 见评论//populate the Combobox,我设置了3项,选择了第3项,以便编译代码并演示问题。 if (cMinutes.Items.Contains(m)) cMinutes.SelectedValue = m; 是我更新 Combobox 的地方,您正在寻找更新 Combobox 的方法,对吧?
    • 我不,我只是想在从 cMinutes 组合框获取值后更新时间文本块中的分钟
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多