【问题标题】:How to get timer running even when application is running in background or phone is locked in Windows Phone即使应用程序在后台运行或手机被锁定在 Windows Phone 中,如何让计时器运行
【发布时间】:2014-01-12 09:03:53
【问题描述】:

我在 Windows Phone 7.1 的应用程序中有一个计时器,使用

实现
DispatcherTimer _timer;

初始化为

Sample._timer = new DispatcherTimer();
Sample._timer.Interval = new TimeSpan(0, 0, 1);
Sample._timer.Tick += new EventHandler(Timer_Tick);
Sample._timer.Start();

    private void Timer_Tick(object sender, EventArgs e)
    {

        double newValue = Sample.Value + 1.686;
        if (newValue >= 100)
            newValue = 0;
        Sample.Value = newValue;
        txtDigitalClock.Text = GetTime();
    }
    public string GetTime()
    {
        time += TimeSpan.FromSeconds(1);
        return string.Format("{0:D2}:{1:D2}:{2:D2}", time.Hours, time.Minutes, time.Seconds);
    }

这在正常情况下工作正常

这是我的问题

1) 手机处于锁定状态(屏幕锁定)时,定时器不运行

2) 应用程序在后台运行时定时器未运行(当您在 windows phone 中按下开始按钮时,应用程序进入后台)。

任何帮助将不胜感激..

【问题讨论】:

    标签: c# windows-phone-8 timer windows-phone-7.1


    【解决方案1】:

    要在锁定屏幕下运行您的应用程序(和计时器),您必须禁用 ApplicationIdleDetectionMode
    如果您不禁用空闲,您的应用程序将停止,正如 MSDN:

    This event (Deactivation) is also raised if the device’s lock screen is engaged, unless application idle detection is disabled.

    所说 如果您想在后台运行 Timer(例如,在按下开始按钮后),您将无法执行此操作,因为 MSDN 说:

    When the user navigates forward, away from an app, after the Deactivated event is raised, the operating system will attempt to put the app into a dormant state. In this state, all of the application’s threads are stopped and no processing takes place, but the application remains intact in memory.

    更大的问题是当您的应用程序被墓碑化时 - 应用程序不会(全部)在内存中。

    您可以尝试使用 Background Agents 完成您的工作,但那是另一回事。

    当您的应用禁用 Idle 或使用后台代理时,还要记住 Certification requirements

    类似的问题是here

    【讨论】:

      【解决方案2】:

      我在谷歌上搜索了你的问题(因为我没有进入 Winphone)并找到了

      http://stackoverflow.com/questions/8352515/how-can-i-run-my-windows-phone-application-in-background

      显然这是不可能的。 我希望这能回答你的问题

      【讨论】:

        【解决方案3】:

        请在下面写下定时器初始化

        ApplicationIdleModeHelper.Current.HasUserAgreedToRunUnderLock = true;
        

        【讨论】:

          【解决方案4】:

          我通过将值的开始计时器保存在独立存储上解决了这个问题

           IsolatedStorageSettings.ApplicationSettings.Add("TimerStarted",DateTime.UtcNow);
          

          当应用程序在进入后台后重新激活时,我将在隔离存储中查找此值并使用它来显示计时器

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-12-17
            • 1970-01-01
            • 1970-01-01
            • 2014-01-18
            • 1970-01-01
            相关资源
            最近更新 更多