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

.NET MAUI Android端应用关闭时Deep Link无法触发SendOnAppLinkRequestReceived

.NET MAUI Android深度链接:解决应用关闭状态下无法触发SendOnAppLinkRequestReceived的问题

问题核心是应用从关闭状态启动时,MAUI上下文尚未初始化完成,直接调用SendOnAppLinkRequestReceived会抛出MauiContext should have been set on parent错误。以下是修复方案:

1. 修改MainActivity代码,延迟处理深度链接

移除OnCreate中直接调用OnNewIntent的逻辑,改为先保存深度链接,等待MAUI上下文初始化完成后再触发事件:

[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    AutoVerify = true, DataScheme = "test-app123")]
[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    AutoVerify = true, DataScheme = "https", DataHost = "test.app123.app")]
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true,
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density,
    LaunchMode = LaunchMode.SingleTask
)]
public class MainActivity : MauiAppCompatActivity
{
    private Uri _pendingDeepLink;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // 处理启动时的Intent
        ExtractDeepLinkFromIntent(Intent);
    }

    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        // 处理后台唤起的Intent
        ExtractDeepLinkFromIntent(intent);
    }

    protected override void OnMauiContextCreated(MauiContext mauiContext)
    {
        base.OnMauiContextCreated(mauiContext);
        // MAUI上下文初始化完成,触发延迟的深度链接
        TriggerPendingDeepLink();
    }

    private void ExtractDeepLinkFromIntent(Intent intent)
    {
        if (intent?.Action != Intent.ActionView)
            return;

        var dataString = intent.DataString;
        if (string.IsNullOrWhiteSpace(dataString))
            return;

        _pendingDeepLink = new Uri(dataString);
    }

    private void TriggerPendingDeepLink()
    {
        if (_pendingDeepLink == null)
            return;

        App.Current.SendOnAppLinkRequestReceived(_pendingDeepLink);
        _pendingDeepLink = null;
    }
}

2. 关键说明

  • OnMauiContextCreated方法:这是MAUI提供的生命周期方法,会在MAUI上下文完全初始化后调用,此时调用App.Current的方法不会再抛出上下文未设置的错误。
  • LaunchMode.SingleTask:保持该配置,确保应用从后台唤起时不会重建Activity,而是调用OnNewIntent。
  • 分离Intent处理逻辑:将提取深度链接的逻辑抽离为单独方法,统一处理OnCreate(首次启动)和OnNewIntent(后台唤起)两种场景。

3. 优化App.xaml.cs的事件处理

确保异步导航逻辑在UI线程执行,避免跨线程错误:

protected async override void OnAppLinkRequestReceived(Uri uri)
{
    Console.WriteLine(uri.ToString());
    Package package = new Package() { packageUri = uri };

    var navigationParameter = new Dictionary<string, object> { { "MyPackage", package } };
    // 确保在UI线程执行导航操作
    await MainThread.InvokeOnMainThreadAsync(async () =>
    {
        await Shell.Current.GoToAsync($"{nameof(NewPage1)}", navigationParameter);
    });
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 05:44:52