如何只运行一个WPF应用程序实例&;从任务栏打开最后一个窗口

本文关键字:amp 任务栏 窗口 最后一个 实例 应用程序 运行 WPF 一个 | 更新日期: 2023-09-27 18:25:41

如何只实现一个WPF应用程序实例来运行并打开任务栏上的窗口?我知道网上有很多关于这个话题的问题,但没有一个明确的答案(或者至少在WPF上有任何答案对我有效)。我对这个问题的答案进行了编程,这是我的代码:

private static Mutex mutex = new Mutex(true, "{1111werqwfwf}");
private static MainWindow mainWindow = null;
App()
{
    InitializeComponent();
}
[STAThread]
static void Main()
{
    if (mutex.WaitOne(TimeSpan.Zero, true))
    {
        App app = new App();
        mainWindow = new MainWindow();
        app.Run(mainWindow);
        mutex.ReleaseMutex();
    }
    else
    {
        mainWindow.WindowState = WindowState.Normal;
    }
}

问题是我的MainWindow没有打开。此外,我需要打开PointOfSale窗口,它是最小化到任务栏的窗口,这是我的这个窗口的代码(我使用的是NotifyIcon插件):

public partial class PointOfSale : Window
{
    TaskbarIcon tb;
    public PointOfSale()
    {
        InitializeComponent();
        tb = (TaskbarIcon)FindResource("NotifyIcon");
        tb.DoubleClickCommand = new ShowWindowCommand();
        tb.Visibility = System.Windows.Visibility.Visible;
        Utils.Utils.SetTaskBarIcon(tb);
   }
}

当PointOfSale关闭时,我检查关闭事件并将其隐藏:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        Utils.Utils.pointOfSale = this;
        Hide();
        e.Cancel = true;
    }

Utils.Utils.pointOfSale = this;上,我保存实际窗口的实例,这样我可以在双击任务栏上的图标时打开它。

关于这方面的信息会很有帮助。

编辑:我认为如果应用程序的最后一个实例可以被杀死,并且可以创建新的一个实例,那么它也可以工作。

如何只运行一个WPF应用程序实例&;从任务栏打开最后一个窗口

从一个WPF应用程序的最简单的实现开始,让它作为一个单例运行,然后您可以将该实现重新合并到您正在构建的任何复杂应用程序中。

这是一个你可以使用的大纲。这确实在SO…上解释了很多次

在VS中生成一个样板WPF应用程序,并按如下方式修改App.xaml.cs(并注意代码中建议进行一些额外更改的注释)。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
namespace Singleton
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    /// <remarks>
    /// 1. Change Build Action for App.xaml (right-click, Properties)
    ///     to Page from ApplicaitonDefinition
    /// 2. Go to project settings, and in the Debug tab, uncheck 
    ///     "Enable Visual Studio Hosting Process"
    /// </remarks>
    public partial class App : Application
    {
        [STAThread]
        public static void Main(string[] args)
        {
            bool bNew = true;
            using (Mutex mutex = new Mutex(true, "Singleton-GUID", out bNew)) // Replace string "Singleton_GUID" with one with a real GUID
            {
                if(bNew)
                {
                    new App().Run();
                }
                else
                {
                    Process me = Process.GetCurrentProcess();
                    List<Process> processes = new List<Process>(Process.GetProcesses());
                    var matches = 
                        processes.FindAll((p) => 
                        {
                            return 
                                string.Equals(p.ProcessName, me.ProcessName, StringComparison.InvariantCultureIgnoreCase) && 
                                (p.Id != me.Id);
                        });
                    if (matches.Count == 1)
                    {
                        Process prior = matches[0];
                        if (prior.MainWindowHandle != IntPtr.Zero)
                        {
                            NativeMethods.ShowWindow(prior.MainWindowHandle, NativeMethods.SH_SHOW);
                        }
                    }
                }
            }
        }
        private App()
        {
            InitializeComponent();
        }
    }
    public static class NativeMethods
    {
        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        public const int SH_SHOW = 5;
    }
}