我目前正在开发适用于 Windows 的 .NET MAUI Blazor 混合应用程序,并且需要跟踪 Outlook 应用程序中的用户活动,特别是确定用户是否花时间阅读邮件或回复邮件。 最近我可以监控用户是否主动使用outlook,但无法找到用户后台或在使用outlook时写任何邮件,请帮助我实现这一目标。
到目前为止,我已经能够使用以下代码计算应用程序的焦点时间: 我创建了一个 WindowsApplicationMonitor.cs 类,它可以帮助我识别用户是否主动使用 Outlook。
using System;
using System.Runtime.InteropServices;
using MAUIDemo.Services;
namespace MAUIDemo.PlatformServices.Windows
{
public class WindowsApplicationMonitor : IApplicationMonitor
{
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
private DateTime? outlookStartTime;
public bool IsApplicationActive(string applicationName)
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(applicationName);
if (processes.Length > 0)
{
// Get the handle of the active window
IntPtr hWnd = GetForegroundWindow();
// Get the process ID associated with the active window
uint processId;
GetWindowThreadProcessId(hWnd, out processId);
// Check if the active window belongs to the application
return processId == processes[0].Id;
}
else
{
return false;
}
}
public TimeSpan GetApplicationActiveTime(string applicationName)
{
if (IsApplicationActive(applicationName))
{
if (outlookStartTime == null)
{
outlookStartTime = DateTime.Now;
}
return DateTime.Now - outlookStartTime.Value;
}
else
{
TimeSpan elapsed = TimeSpan.Zero;
if (outlookStartTime != null)
{
elapsed = DateTime.Now - outlookStartTime.Value;
outlookStartTime = null;
}
return elapsed;
}
}
}
}
在我的 .NET MAUI Blazor 项目的 .razor 页面中,我有以下内容: 当用户从 Outlook 中失去焦点时,此处的数据将更新。
@page "/nonitor"
@inject MAUIDemo.Services.IApplicationMonitor ApplicationMonitor
@using System.Timers
@using MAUIDemo.Services
@inject MAUIDemo.Services.IApplicationMonitor ApplicationMonitor
@inject MAUIDemo.Services.ActivePeriodService ActivePeriodService
@using System.Timers
<h3>Outlook Status</h3>
<button class="btn btn-primary" @onclick="ClearList">Clear List</button>
<p>@statusMessage</p>
<table class="table">
<thead>
<tr>
<th>Start Time</th>
<th>End Time</th>
<th>Duration (hh:mm:ss)</th>
</tr>
</thead>
<tbody>
@foreach (var period in activePeriods)
{
<tr>
<td>@period.Start</td>
<td>@period.End</td>
<td>@String.Format("{0:D2}:{1:D2}:{2:D2}", period.Duration.Hours, period.Duration.Minutes, period.Duration.Seconds)</td>
</tr>
}
</tbody>
</table>
@code {
private string statusMessage = "";
//private List<ActivePeriod> activePeriods = new List<ActivePeriod>();
private List<ActivePeriod> activePeriods => ActivePeriodService.ActivePeriods;
private ActivePeriod currentPeriod;
private Timer timer;
protected override void OnInitialized()
{
timer = new Timer(1000); // Update every second
timer.Elapsed += UpdateStatus;
timer.Start();
}
private bool lastStatus = false; // keeps track of the last known status of the Outlook application (whether it was active or not).
private void UpdateStatus(Object source, ElapsedEventArgs e)
{
var isOutlookActive = ApplicationMonitor.IsApplicationActive("OUTLOOK");
if (isOutlookActive != lastStatus) // Only update the status message and UI when the status changes
{
statusMessage = isOutlookActive ? "Outlook is active." : "Outlook is not active.";
if (isOutlookActive)
{
if (currentPeriod == null)
{
currentPeriod = new ActivePeriod { Start = DateTime.Now };
}
}
else
{
if (currentPeriod != null)
{
// Create a new instance when adding to the list
activePeriods.Add(new ActivePeriod { Start = currentPeriod.Start, End = DateTime.Now });
currentPeriod = null; // This will ensure a new instance is created each time
}
}
InvokeAsync(() => StateHasChanged()); // Update the UI
}
lastStatus = isOutlookActive; // Remember the last status for the next time
}
public void Dispose()
{
timer?.Dispose();
}
public class ActivePeriod
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
public TimeSpan Duration => End - Start;
}
void ClearList()
{
ActivePeriodService.ActivePeriods.Clear();
}
}
我想在用户主动使用 Outlook 时监控用户读取或写入电子邮件的情况。
您需要使用 Outlook 对象模型(Outlook.Application 对象)。然后,您可以订阅在用户选择消息 (
Explorer.SelectionChange
)、打开消息(新消息或在检查器中回复 - Application.Inspectors.NewInspector
)、发送消息 (Application.ItemSend
) 等时触发的各种 Outlook 事件。