C# 如何计算用户在WP7 silverlight应用程序上花费的总时间?

C# 如何计算用户在WP7 silverlight应用程序上花费的总时间?,c#,silverlight,windows-phone-7,C#,Silverlight,Windows Phone 7,我需要将用户在应用程序上花费的总时间保存到一个独立的存储中 我想我应该知道开始和结束执行的时间 如果有什么办法,请 谢谢..App.xaml.cs文件中有两种方法与执行模型直接相关: 应用程序启动 应用程序已激活 应用程序已停用 应用程序关闭 您可以钩住这个方法,并提供所需的逻辑,实际上非常简单: 应用程序启动-初始化spentTime 应用程序\u已激活-从还原spentTime 应用程序_停用-重新计算时间并存储,以便能够进一步获取时间 应用程序关闭-重新计算并将spentTime存储

我需要将用户在应用程序上花费的总时间保存到一个独立的存储中

我想我应该知道开始和结束执行的时间

如果有什么办法,请


谢谢..

App.xaml.cs文件中有两种方法与执行模型直接相关:

  • 应用程序启动
  • 应用程序已激活
  • 应用程序已停用
  • 应用程序关闭
您可以钩住这个方法,并提供所需的逻辑,实际上非常简单:

  • 应用程序启动-初始化
    spentTime
  • 应用程序\u已激活-从还原
    spentTime
  • 应用程序_停用-重新计算时间并存储,以便能够进一步获取时间
  • 应用程序关闭-重新计算并将
    spentTime
    存储到

了解WP执行模型的一些有用链接:


App.xaml.cs文件中有两种方法与执行模型直接相关:

  • 应用程序启动
  • 应用程序已激活
  • 应用程序已停用
  • 应用程序关闭
您可以钩住这个方法,并提供所需的逻辑,实际上非常简单:

  • 应用程序启动-初始化
    spentTime
  • 应用程序\u已激活-从还原
    spentTime
  • 应用程序_停用-重新计算时间并存储,以便能够进一步获取时间
  • 应用程序关闭-重新计算并将
    spentTime
    存储到

了解WP执行模型的一些有用链接:


要添加到AnatoliiG的答案中,您可以使用类来计算时间。使用DateTime时要小心。现在来计算用户使用该应用程序的时间,因为在一年中的某些时候,这会给你带来不好的值

private _DispatcherTimer _timer;
private int _spentTime;

public Application()
{
    _timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
    _timer.Tick += TimerTick;
}

TimerTick(object s, EventArgs args)
{
    _spentTime++;
}
然后按照AnatoliiG的例子来节省在不同活动上花费的时间

private void Application_Launching(object sender, LaunchingEventArgs e)
{
    _timer.Start();
    // Should probably have some logic to determine if they tombstoned the app
    // and did not actually leave the app, if so then save that time
}

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    _timer.Start();
    // Restore _spentTime
}

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    _timer.Stop();
    // Store _spentTime
}

private void Application_Closing(object sender, ClosingEventArgs e)
{
    // Save time, they're done!
}

要添加到AnatoliiG的答案中,可以使用类来计算时间。使用DateTime时要小心。现在来计算用户使用该应用程序的时间,因为在一年中的某些时候,这会给你带来不好的值

private _DispatcherTimer _timer;
private int _spentTime;

public Application()
{
    _timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
    _timer.Tick += TimerTick;
}

TimerTick(object s, EventArgs args)
{
    _spentTime++;
}
然后按照AnatoliiG的例子来节省在不同活动上花费的时间

private void Application_Launching(object sender, LaunchingEventArgs e)
{
    _timer.Start();
    // Should probably have some logic to determine if they tombstoned the app
    // and did not actually leave the app, if so then save that time
}

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    _timer.Start();
    // Restore _spentTime
}

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    _timer.Stop();
    // Store _spentTime
}

private void Application_Closing(object sender, ClosingEventArgs e)
{
    // Save time, they're done!
}