MAUI怎么打开另一个应用 MAUI Launcher API用法

来源:这里教程网 时间:2026-02-21 17:36:59 作者:

在 MAUI 中打开另一个应用,主要靠 Launcher API(`Microsoft.Maui.Controls.Launcher`),它封装了平台底层的“启动外部应用”能力,比如打开微信、浏览器、邮件客户端,或用自定义 URL Scheme 启动其他 App。

确认目标应用支持外部调用

不是所有 App 都能被随意打开。你需要先确认目标 App 是否注册了可被调用的协议(URL Scheme)或 Intent(Android)/Universal Link(iOS)。例如:

微信:`weixin://`(部分功能受限,iOS 上需白名单) 系统浏览器:`https://example.com`(会被系统默认浏览器处理) 邮件:`mailto:test@example.com` 自定义 App:如 `myapp://open?param=1`(需对方 App 在 AndroidManifest.xml 或 Info.plist 中正确声明)

使用 Launcher.OpenAsync() 启动链接或应用

这是最常用的方式,适用于已知 URL Scheme 或标准 URI(如 http、mailto、tel):

try
{
    bool canOpen = await Launcher.CanOpenAsync("https://microsoft.com");
    if (canOpen)
    {
        await Launcher.OpenAsync("https://microsoft.com");
    }
    else
    {
        // 提示用户手动安装或打开方式
        await Toast.Make("无法打开链接").Show();
    }
}
catch (Exception ex)
{
    // 处理异常(如未注册 scheme、权限拒绝等)
}

注意:Android 12+ 和 iOS 要求目标 scheme 必须提前在清单文件中声明或通过 App Store 审核白名单,否则 `CanOpenAsync` 返回 false 或直接失败。

Android 特殊情况:用 Intent 显式启动(进阶)

若目标 App 提供了明确的包名(package name)和 Activity 名,且你有权限(如同一开发者签名或已适配 Android 11+ 的

<queries></queries>
),可借助平台原生能力:

Platforms/Android/AndroidManifest.xml
<manifest></manifest>
标签下添加查询声明:
<queries>
  <package android:name="com.example.otherapp" />
</queries>
然后在 C# 中调用(需平台判断):
#if ANDROID
var intent = new Android.Content.Intent();
intent.SetPackage("com.example.otherapp");
intent.SetAction(Android.Content.Intent.ActionMain);
intent.AddCategory(Android.Content.Intent.CategoryLauncher);
intent.AddFlags(Android.Content.Intent.FlagActivityNewTask);
<p>try
{
Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.StartActivity(intent);
}
catch (Android.Content.ActivityNotFoundException)
{
// App 未安装
}</p><h1>endif

iOS 注意事项:LSApplicationQueriesSchemes 和 Universal Links

iOS 更严格:

若用自定义 scheme(如
myapp://
),必须在
Platforms/iOS/Info.plist
中添加
LSApplicationQueriesSchemes
数组,列出允许查询的 scheme:
<key>LSApplicationQueriesSchemes</key>
<array>
  <string>myapp</string>
  <string>weixin</string>
</array>
推荐优先使用 Universal Links(https 链接 + 关联域名),体验更好、无需 scheme 白名单,但需要对方 App 和服务器配合配置。

基本上就这些。Launcher API 看似简单,实际成败关键在目标 App 的配置和平台合规性。别跳过

CanOpenAsync
检查,也别忽略各平台的清单声明——漏掉一个,就可能在真机上静默失败。

相关推荐