/// <summary> /// Tracks information about the app's launch. /// </summary> /// <param name="args">Details about the launch request and process.</param> /// <param name="xamlRoot">The XamlRoot object from your visual tree.</param> public void TrackAppUse(IActivatedEventArgs args, XamlRoot xamlRoot = null) { if (args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser || args.PreviousExecutionState == ApplicationExecutionState.NotRunning) { LaunchCount = _localObjectStorageHelper.Read <long>(nameof(LaunchCount)) + 1; TotalLaunchCount = _localObjectStorageHelper.Read <long>(nameof(TotalLaunchCount)) + 1; // In case we upgraded the properties, make TotalLaunchCount is correct if (TotalLaunchCount < LaunchCount) { TotalLaunchCount = LaunchCount; } _localObjectStorageHelper.Save(nameof(LaunchCount), LaunchCount); _localObjectStorageHelper.Save(nameof(TotalLaunchCount), TotalLaunchCount); LaunchTime = DateTime.UtcNow; var lastLaunch = _localObjectStorageHelper.Read <long>(nameof(LastLaunchTime)); LastLaunchTime = lastLaunch != default(long) ? DateTime.FromFileTimeUtc(lastLaunch) : LaunchTime; _localObjectStorageHelper.Save(nameof(LastLaunchTime), LaunchTime.ToFileTimeUtc()); _localObjectStorageHelper.Save(nameof(AppUptime), 0L); var lastResetTime = _localObjectStorageHelper.Read <long>(nameof(LastResetTime)); LastResetTime = lastResetTime != default(long) ? DateTime.FromFileTimeUtc(lastResetTime) : DateTime.MinValue; } if (xamlRoot != null) { void XamlRoot_Changed(XamlRoot sender, XamlRootChangedEventArgs e) { UpdateVisibility(sender.IsHostVisible); } xamlRoot.Changed -= XamlRoot_Changed; xamlRoot.Changed += XamlRoot_Changed; } else { void App_VisibilityChanged(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.VisibilityChangedEventArgs e) { UpdateVisibility(e.Visible); } Windows.UI.Core.CoreWindow.GetForCurrentThread().VisibilityChanged -= App_VisibilityChanged; Windows.UI.Core.CoreWindow.GetForCurrentThread().VisibilityChanged += App_VisibilityChanged; } }
/// <summary> /// Tracks information about the app's launch. /// </summary> /// <param name="args">Details about the launch request and process.</param> public static void TrackAppUse(LaunchActivatedEventArgs args) { if (args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser || args.PreviousExecutionState == ApplicationExecutionState.NotRunning) { LaunchCount = _localObjectStorageHelper.Read <long>(nameof(LaunchCount)) + 1; TotalLaunchCount = _localObjectStorageHelper.Read <long>(nameof(TotalLaunchCount)) + 1; // In case we upgraded the properties, make TotalLaunchCount is correct if (TotalLaunchCount < LaunchCount) { TotalLaunchCount = LaunchCount; } _localObjectStorageHelper.Save(nameof(LaunchCount), LaunchCount); _localObjectStorageHelper.Save(nameof(TotalLaunchCount), TotalLaunchCount); LaunchTime = DateTime.UtcNow; var lastLaunch = _localObjectStorageHelper.Read <long>(nameof(LastLaunchTime)); LastLaunchTime = lastLaunch != default(long) ? DateTime.FromFileTimeUtc(lastLaunch) : LaunchTime; _localObjectStorageHelper.Save(nameof(LastLaunchTime), LaunchTime.ToFileTimeUtc()); _localObjectStorageHelper.Save(nameof(AppUptime), 0L); var lastResetTime = _localObjectStorageHelper.Read <long>(nameof(LastResetTime)); LastResetTime = lastResetTime != default(long) ? DateTime.FromFileTimeUtc(lastResetTime) : DateTime.MinValue; } void App_VisibilityChanged(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.VisibilityChangedEventArgs e) { if (e.Visible) { _sessionStart = DateTime.UtcNow; } else { var subsessionLength = DateTime.UtcNow.Subtract(_sessionStart).Ticks; var uptimeSoFar = _localObjectStorageHelper.Read <long>(nameof(AppUptime)); _localObjectStorageHelper.Save(nameof(AppUptime), uptimeSoFar + subsessionLength); } } Windows.UI.Core.CoreWindow.GetForCurrentThread().VisibilityChanged -= App_VisibilityChanged; Windows.UI.Core.CoreWindow.GetForCurrentThread().VisibilityChanged += App_VisibilityChanged; }
/// <summary> /// Track app launch information /// </summary> /// <param name="args">Details about the launch request and process.</param> public static void TrackAppUse(LaunchActivatedEventArgs args) { if (args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser || args.PreviousExecutionState == ApplicationExecutionState.NotRunning) { if (ApplicationData.Current.LocalSettings.Values.TryGetValue(nameof(LaunchCount), out object launchCount)) { LaunchCount = (long)launchCount + 1; } else { LaunchCount = 1; } ApplicationData.Current.LocalSettings.Values[nameof(LaunchCount)] = LaunchCount; LaunchTime = DateTime.UtcNow; if (ApplicationData.Current.LocalSettings.Values.TryGetValue(nameof(LastLaunchTime), out object lastLaunch)) { LastLaunchTime = DateTime.FromFileTimeUtc((long)lastLaunch); } ApplicationData.Current.LocalSettings.Values[nameof(LastLaunchTime)] = LaunchTime.ToFileTimeUtc(); ApplicationData.Current.LocalSettings.Values[nameof(AppUptime)] = 0L; } void App_VisibilityChanged(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.VisibilityChangedEventArgs e) { if (e.Visible) { _sessionStart = DateTime.UtcNow; } else { var subsessionLength = DateTime.UtcNow.Subtract(_sessionStart).Ticks; ApplicationData.Current.LocalSettings.Values.TryGetValue(nameof(AppUptime), out object uptimeSoFar); ApplicationData.Current.LocalSettings.Values[nameof(AppUptime)] = (long)uptimeSoFar + subsessionLength; } } Windows.UI.Core.CoreWindow.GetForCurrentThread().VisibilityChanged -= App_VisibilityChanged; Windows.UI.Core.CoreWindow.GetForCurrentThread().VisibilityChanged += App_VisibilityChanged; }