跳到主要内容

用 C# 实现

这里是 Napacitor 与 Capacitor 差异最大之处:我们不需要为 iOS 写一个 Swift 文件、为 Android 写一个 Kotlin 文件,而是编写一个 C# 类,让它在 Android、iOS、macOS 与 Windows 上的 MAUI 宿主中运行。

ScreenOrientation 的 Web 实现已完成。现在我们用 [NapacitorPlugin] 模型实现原生侧。

创建插件类​

在 nap add maui 生成的宿主工程中创建文件 maui/ScreenOrientationPlugin.cs:

using Microsoft.Maui.Devices;
using Napacitor.Plugins;

namespace DeliveryApp.Maui.Plugins;

/// <summary>屏幕方向插件:读取当前方向并监听变化。</summary>
[NapacitorPlugin("ScreenOrientation")]
public class ScreenOrientationPlugin : NapacitorPluginBase
{
private const string OrientationChangeEvent = "screenOrientationChange";

[NapacitorMethod]
public Task<ScreenOrientationResult> Orientation()
{
var info = DeviceDisplay.Current.MainDisplayInfo;
return Task.FromResult(new ScreenOrientationResult
{
Type = Map(info.Orientation),
});
}

protected override void OnListenerAdded(string eventName)
{
if (eventName == OrientationChangeEvent)
DeviceDisplay.Current.MainDisplayInfoChanged += OnDisplayInfoChanged;
}

protected override void OnListenerRemoved(string eventName)
{
if (eventName == OrientationChangeEvent)
DeviceDisplay.Current.MainDisplayInfoChanged -= OnDisplayInfoChanged;
}

private void OnDisplayInfoChanged(object? sender, DisplayInfoChangedEventArgs e)
{
NotifyListeners(OrientationChangeEvent, new ScreenOrientationResult
{
Type = Map(e.DisplayInfo.Orientation),
});
}

private static string Map(DisplayOrientation orientation) => orientation switch
{
DisplayOrientation.Portrait => "portrait",
_ => "landscape",
};
}

/// <summary>方向结果(序列化为 { type: "portrait" | "landscape" })。</summary>
public class ScreenOrientationResult
{
public string Type { get; set; } = string.Empty;
}

我们逐点拆解一下:

  • [NapacitorPlugin("ScreenOrientation")] 以名称 "ScreenOrientation" 注册该类——与 JS 侧传给 registerPlugin 的名称相同。运行时通过反射发现该类,因此无需再注册任何内容。
  • [NapacitorMethod] 把 Orientation() 暴露给 JavaScript。每个标记了该特性的公开方法都可从 JS 调用;未标记的方法只保留在 C# 内部。
  • 该方法返回 Task<ScreenOrientationResult>。桥接层会 await 该任务,并把结果序列化为camelCase 属性名的 JSON,因此 Type 在 JavaScript 中呈现为 type——正是 definitions.ts 中 ScreenOrientationResult 所期望的。
  • DeviceDisplay 来自 Microsoft.Maui.Essentials,在四个平台上的行为完全一致。不需要 #if 块,也不需要按平台划分的工程。

向 JavaScript 发送事件​

ScreenOrientation.orientation() 只在被调用时执行;应用还需要在页面加载后方向发生变化时得到通知。这正是事件管道的用途:

  • NotifyListeners("screenOrientationChange", data) 把负载推送给通过 addListener 注册的每个 JS 监听器。
  • OnListenerAdded / OnListenerRemoved 会在第一个 JS 监听器注册以及最后一个注销时触发。只在这两个钩子内订阅原生事件源(DeviceDisplay.MainDisplayInfoChanged),可以让插件在无人监听时不做无用功。
  • ListenerCount("screenOrientationChange") 返回当前挂载的 JS 监听器数量。

JS 侧接收负载的方式与 Web 实现完全一致:

ScreenOrientation.addListener('screenOrientationChange', res => {
// res.type === 'portrait' | 'landscape'
});

测试 C# 实现​

构建 Web 产物、同步,然后在 MAUI 宿主中运行应用:

npm run build
npx nap sync
npx nap open windows # 或:npx nap open android
npx nap run windows

nap sync 会把构建好的 Web 应用复制到宿主工程,并重新生成 maui/napacitor.build.props。当你旋转设备(或在桌面端调整窗口大小)时,界面应该像在浏览器中一样切换设计。

如果在宿主内看到 ScreenOrientation does not have web implementation 或 unimplemented 错误,说明插件类未被发现。请检查该类是否为 public 且带有公开无参构造函数、程序集名称是否以 Napacitor 开头,以及添加文件后是否运行过 nap sync。参见宿主发现要求。

作为本地插件,它已经功能完整。接下来:把它打包成全局 npm 包,以便其他应用安装使用。