我正在开发一个没有主窗口的 WPF 应用程序(它使用来自http://www.codeproject.com/KB/WPF/wpf_notifyicon.aspx的代码在通知区域中运行)。
在 App.xaml.cs 中,我创建了一个新线程,它运行一些返回自定义警报集合的监控代码。该集合有一个 render() 方法,我计划用它来显示一个包含警报信息的窗口,但我不知道如何完成它。如有任何意见,我将不胜感激。
下面的代码示例:
应用程序.xaml:
<Application x:Class="DowntimeReportMonitor.Views.Icon.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup"
Exit="Application_Exit">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="IconDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
应用程序.xaml.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading;
using System.Windows;
using Hardcodet.Wpf.TaskbarNotification;
using DowntimeReportMonitor.Core;
namespace DowntimeReportMonitor.Views.Icon
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private TaskbarIcon _taskbaricon;
private AlertWorker _alertWorker;
private Thread _alertWorkerThread;
/// <summary>
/// Event handler for Application startup
/// </summary>
/// <param name="sender">The event sender</param>
/// <param name="e">Event arguments</param>
private void Application_Startup(object sender, StartupEventArgs e)
{
// Create and start a new TaskbarIcon.
this._taskbaricon = (TaskbarIcon)FindResource("notificationIcon");
// Create and start a new AlertWorker.
this._alertWorker = new AlertWorker();
this._alertWorkerThread = new Thread(this._alertWorker.doWork);
this._alertWorkerThread.SetApartmentState(ApartmentState.STA);
this._alertWorkerThread.Start();
}
/// <summary>
/// Event handler for Application exit
/// </summary>
/// <param name="sender">The event sender</param>
/// <param name="e">Event arguments</param>
private void Application_Exit(object sender, ExitEventArgs e)
{
// Stop the alert worker.
this._alertWorker.requestStop();
this._alertWorkerThread.Join();
// Dispose of the notification icon.
this._taskbaricon.Dispose();
}
}
}
警报工作者.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using DowntimeReportMonitor.Core;
namespace DowntimeReportMonitor.Views.Icon
{
class AlertWorker
{
private volatile bool _stopRequested;
private ReportMonitor _reportMonitor;
public AlertWorker()
{
_reportMonitor = new ReportMonitor(new wpfRenderableReportAlertCollection());
}
public void doWork()
{
while (!_stopRequested)
{
this._reportMonitor.monitorReports().render();
Thread.Sleep(30000);
}
}
public void requestStop()
{
this._stopRequested = true;
}
}
}