在没有主窗口的WPF应用程序中从一个新线程创建一个窗口

本文关键字:一个 窗口 创建 线程 新线程 WPF 应用程序 | 更新日期: 2023-09-27 18:14:08

我正在开发一个没有主窗口的WPF应用程序(它使用http://www.codeproject.com/KB/WPF/wpf_notifyicon.aspx中的代码在通知区域运行)

在App.xaml.cs中,我创建了一个新线程,它运行一些返回自定义警报集合的监视代码。这个集合有一个render()方法,我计划用它来显示一个包含警报信息的窗口,但是我不知道如何完成它。如有任何意见,我将不胜感激。

下面的代码示例:

App.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>

App.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();
    }
}
}

AlertWorker.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;
    }
}
}

在没有主窗口的WPF应用程序中从一个新线程创建一个窗口

首先,您需要一个专用的UI线程。通常是初始应用程序线程,但您可以使用任何线程。(必须是STA线程)

接下来,在该线程中启动分派器(Dispatcher.CurrentDispatcher.Run)。

接下来,您可以使用Dispatcher将命令发送到该线程。

最后,您可以为您的自定义窗口类的线程window.Show发布。