You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows 11中MAUI应用从任务栏打开需双击,求单击实现方案

MAUI应用Windows 11任务栏单击恢复窗口解决方案

针对MAUI应用在Windows 11系统中最小化到任务栏后需双击才能恢复的问题,可通过Windows平台特定代码适配系统行为,以下是两种可行方案:

方案一:监听窗口激活事件

在Windows项目的MainWindow.xaml.cs中,通过监听窗口激活状态变更,主动恢复窗口:

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Windowing;
using WinRT.Interop;

namespace YourAppName.WinUI;

public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
        // 获取当前窗口的AppWindow实例
        var hWnd = WindowNative.GetWindowHandle(this);
        var windowId = Win32Interop.GetWindowIdFromWindow(hWnd);
        _ = AppWindow.GetFromWindowId(windowId);
        
        // 绑定窗口激活事件
        this.Activated += MainWindow_Activated;
    }

    private void MainWindow_Activated(object sender, WindowActivatedEventArgs args)
    {
        // 当窗口处于最小化状态且收到激活信号时,恢复正常窗口状态
        if (this.WindowState == WindowState.Minimized 
            && args.WindowActivationState != WindowActivationState.Deactivated)
        {
            this.WindowState = WindowState.Normal;
        }
    }
}

方案二:Hook窗口消息处理

通过拦截Windows原生窗口消息,处理任务栏单击触发的恢复指令:

using System.Runtime.InteropServices;
using Microsoft.UI.Xaml;
using WinRT.Interop;

namespace YourAppName.WinUI;

public sealed partial class MainWindow : Window
{
    private delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
    private WndProc _oldWndProc;
    private IntPtr _hWnd;

    public MainWindow()
    {
        this.InitializeComponent();
        _hWnd = WindowNative.GetWindowHandle(this);
        // 替换窗口消息处理函数
        _oldWndProc = (WndProc)Marshal.GetDelegateForFunctionPointer(
            GetWindowLongPtr(_hWnd, GWLP_WNDPROC), typeof(WndProc));
        SetWindowLongPtr(_hWnd, GWLP_WNDPROC, 
            Marshal.GetFunctionPointerForDelegate((WndProc)WndProcHook));
    }

    private IntPtr WndProcHook(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
    {
        const uint WM_SYSCOMMAND = 0x0112;
        const uint SC_RESTORE = 0xF120;

        if (msg == WM_SYSCOMMAND && wParam.ToInt32() == SC_RESTORE)
        {
            // 强制恢复窗口状态
            this.WindowState = WindowState.Normal;
            return IntPtr.Zero;
        }

        // 调用原消息处理函数
        return _oldWndProc(hWnd, msg, wParam, lParam);
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);

    private const int GWLP_WNDPROC = -4;
}

注意事项

  • 两种方案均需在Windows平台项目中实现,属于MAUI的平台特定代码,不会影响其他平台功能。
  • 测试时若遇到权限问题,可尝试以管理员权限运行应用,避免消息拦截或窗口状态变更失败。

内容的提问来源于stack exchange,提问作者Sagar S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 00:05:20