我想在我的. 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);
}
}
方法每次都会调用.所以这不是问题所在.问题是,当应用程序应该切换边和结束时出现错误.