我想在我的. net毛伊岛应用程序中实现深度链接.我在物理Android设备上进行测试.当应用程序关闭时,一切工作正常,但如果应用程序仍然在后台打开,我打开一个链接,我会得到错误"窗口已创建".我遵循Microsoft(https://learn.microsoft.com/en-us/dotnet/maui/android/app-links?view=net-maui-8.0)的标准指南

这是我的Mauipprogram.cs:

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .UseMauiCommunityToolkit()
            .UseViewServices()
            .UseMicrocharts()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            })
            .ConfigureLifecycleEvents(lifecycle =>
            {
#if IOS || MACCATALYST
#elif ANDROID
                lifecycle.AddAndroid(android => {
                    android.OnCreate((activity, bundle) =>
                    {
                        var action = activity.Intent?.Action;
                        var data = activity.Intent?.Data?.ToString();

                        if (action == Intent.ActionView && data is not null)
                        {
                            HandleAppLink(data);
                        }
                    });
                });
#endif
            });

//...

#if DEBUG
        builder.Logging.AddDebug();
#endif

        return builder.Build();
    }

    static void HandleAppLink(string url)
    {
        if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out var uri))
            App.Current?.SendOnAppLinkRequestReceived(uri);
    }
}

这是我的OnAppLinkRequestReceived:

protected override async void OnAppLinkRequestReceived(Uri uri)
{
    base.OnAppLinkRequestReceived(uri);

    // Überprüft, ob die Anforderung von der Passwort-App stammt
    if (uri.Host == "engelberth-developing" && uri.Segments != null)
    {
        // Behandelt den App-Link für das Teilen von Elementen
        if (uri.Segments.Length == 5 && uri.Segments[2] == "share/")
        {
            Global.Transfer = uri.Segments[3].Replace("/", "|") + uri.Segments[4].Replace("/", "").Replace('*', '/');

            // Setzt die nächste Seite auf die Seite zum Freigeben von Elementen
            await AssignService.SetNextPage<ISharedItemPage>();
        }
        // Behandelt den App-Link für die Synchronisierung von Geräten
        else if (uri.Segments.Length == 4 && uri.Segments[2] == "sync/")
        {
            Global.Transfer = uri.Segments[3].Replace("/", "").Replace('*', '/');

            // Setzt die nächste Seite auf die Seite für die Gerätesynchronisierung
            await AssignService.SetNextPage<IDeviceSyncPage>();
        }
    }
}

这是我的服务功能.它100%用于所有其他操作:

public static async Task SetNextPage<TService>(string paramValue = null)
{
    var implementationType = services
                             .Where(service => service.ServiceType == typeof(TService))
                             .Select(service => service.ImplementationType)
                             .FirstOrDefault();

    if (implementationType != null)
    {
        var route = paramValue != null ? $"/{implementationType.Name}?{paramValue}" : $"/{implementationType.Name}";
        await Shell.Current.GoToAsync(route);
    }
}

方法每次都会调用.所以这不是问题所在.问题是,当应用程序应该切换边和结束时出现错误.

推荐答案

我终于找到了解决办法.您必须将LaunchMode = LaunchMode. SingleTask添加到MainActivity:

[Activity(
    //Theme = "@style/Maui.SplashTheme",
    Theme = "@style/Maui.MainTheme",
    MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize, LaunchMode = LaunchMode.SingleTask)]

然后你必须在MainActivity中实现OnCreate和OnNewIntent方法:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    var action = Intent?.Action;
    var data = Intent?.Data?.ToString();

    if (action == Intent.ActionView && data is not null)
    {
        HandleAppLink(data);
    }
}

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);

    Intent = intent;
    var action = Intent?.Action;
    var data = Intent?.Data?.ToString();

    if (action == Intent.ActionView && data is not null)
    {
        HandleAppLink(data);
    }
}

static void HandleAppLink(string url)
{
    if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out var uri))
        App.Current?.SendOnAppLinkRequestReceived(uri);
}

如果您还实现了intentfilters,则错误"Windows was allready created"将消失,并且当应用程序重新打开和打开时将调用OnAppLinkRequestReceived().

Csharp相关问答推荐

C# uwp中的Win11启动屏幕剪辑工作方式不同

如何注销Microsoft帐户?

Blazor服务器端的身份验证角色

System.Net.Http.HttpClient.SendAsync(request)在docker容器内的POST方法30秒后停止

如何在没有前缀和可选后缀的情况下获取Razor Page Handler方法名称?

附加标题不起作用,而添加则起作用

调用Task.Run()与DoSomethingAsync()有什么不同?

将字节转换为 struct 并返回

用于管理System.Text.Json中的多态反序列化的自定义TypeInfoResolver

DbContext-传递自定义配置选项

如何管理Azure认证客户端响应和证书 fingerprint

LINQ to Entities中的加权平均值

在ASP.NET Core 8 MVC中本地化共享视图

如何在GRPC代码First服务的返回类型上使用多态性?

如何将%{v_扩展}转换为%{v_扩展}>>

为什么ReadOnlySpan;T&>没有Slice(...)的重载接受Range实例的?

DropDownListFor未显示选定值

Azure队列触发器未使用隔离的工作进程执行

Xamarin.Forms项目中缺少MainPage.xaml

从具有泛型类型约束的类继承-Blazor