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

技术咨询:.NET MAUI是否支持窗口覆盖(Windows Overlay)功能?

.NET MAUI窗口覆盖(Windows Overlay)功能说明

.NET MAUI本身没有原生提供跨平台的窗口覆盖API,但可以通过平台特定代码针对不同操作系统实现该功能,以下是各平台的实现思路和示例:

各平台实现方案

Windows平台

可借助Win32 API或WinUI 3能力创建置顶窗口,结合MAUI窗口句柄实现:

#if WINDOWS
using Microsoft.UI.Xaml;
using System.Runtime.InteropServices;

public static class OverlayHelper
{
    [DllImport("user32.dll")]
    private static extern IntPtr CreateWindowEx(int dwExStyle, string lpClassName, string lpWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);

    [DllImport("user32.dll")]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    private const int WS_EX_TOPMOST = 0x00000008;
    private const int WS_POPUP = 0x80000000;
    private const uint SWP_NOMOVE = 0x0002;
    private const uint SWP_NOSIZE = 0x0001;
    private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);

    public static IntPtr CreateOverlayWindow()
    {
        var windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(Application.Current.Windows[0].Handler.PlatformView);
        var overlayHandle = CreateWindowEx(WS_EX_TOPMOST, "static", "MAUI Overlay", WS_POPUP, 0, 0, 300, 200, windowHandle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
        SetWindowPos(overlayHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
        return overlayHandle;
    }
}
#endif

Android平台

使用WindowManager添加悬浮窗口,需先声明SYSTEM_ALERT_WINDOW权限,Android 6.0+还需动态申请权限:

#if ANDROID
using Android.Content;
using Android.Views;
using Android.Widget;

public static class OverlayHelper
{
    public static void ShowOverlay(Context context)
    {
        var windowManager = context.GetSystemService(Context.WindowService) as WindowManager;
        var @params = new WindowManagerLayoutParams(
            WindowManagerLayoutParams.WrapContent,
            WindowManagerLayoutParams.WrapContent,
            WindowManagerTypes.ApplicationOverlay,
            WindowManagerFlags.NotFocusable | WindowManagerFlags.NotTouchModal,
            Android.Graphics.Format.Translucent);

        var overlayView = new TextView(context)
        {
            Text = "MAUI Overlay",
            BackgroundColor = Android.Graphics.Color.Red
        };

        windowManager.AddView(overlayView, @params);
    }
}
#endif

iOS平台

iOS对悬浮窗口限制严格,可通过UIWindow创建额外窗口并设置高windowLevel实现,但需注意App Store审核规则,仅特定场景允许:

#if IOS
using UIKit;

public static class OverlayHelper
{
    public static void ShowOverlay()
    {
        var overlayWindow = new UIWindow(UIApplication.SharedApplication.KeyWindow.Bounds)
        {
            WindowLevel = UIWindowLevel.Alert + 1,
            BackgroundColor = UIColor.FromRGBA(1, 0, 0, 0.5f)
        };
        var label = new UILabel(overlayWindow.Bounds)
        {
            Text = "MAUI Overlay",
            TextAlignment = UITextAlignment.Center
        };
        overlayWindow.AddSubview(label);
        overlayWindow.MakeKeyAndVisible();
    }
}
#endif

注意事项

  • Android:必须在清单文件中声明权限,并引导用户手动开启悬浮窗权限(部分机型需跳转系统设置)
  • iOS:此类实现可能违反App Store审核规范,仅建议在企业级应用或特定合规场景使用
  • 所有实现均为平台特定代码,需通过条件编译隔离不同平台逻辑

内容的提问来源于stack exchange,提问作者Gajendra Singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 19:39:17