如何在.net MAUI Blazor应用程序中处理intent



我试图在。net maui blazor应用程序中处理intent,我已经注册了intent并得到了一切设置,但是当我点击"打开应用程序"时,它只是打开应用程序的根页面,似乎忽略了我的路由逻辑。

我已经实现了OnNewIntent动作在这个答案中描述:https://stackoverflow.com/a/72696842/1662619

protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
var data = intent.DataString;
if (intent.Action != Intent.ActionView) return;
if (string.IsNullOrWhiteSpace(data)) return;
var path = data.Replace(@"https://mydomain.app", "");

//Store the request path to be used in main.razor
NavigationService.SetPage(path);
StartActivity(typeof(MainActivity));
}

但是我不能让它在那里碰到断点(我猜是因为它在一个新实例中打开了应用程序)。

然后在我的Main.razor:

@code
{
[Inject]
public NavigationManager NavigationManager { get; set; }
protected override void OnAfterRender(bool firstRender)
{
var intentPath = PreferencesHandler.GetIntentPath();
if (!string.IsNullOrWhiteSpace(intentPath))
{
NavigationManager.NavigateTo(intentPath);
}
}
}

我试过添加日志记录,但OnNewIntent逻辑似乎永远不会被击中。为什么不呢?

事实证明,你也可以在OnCreate方法中获得意图,像这样:

protected override void OnCreate(Bundle savedInstanceState)
{
Intent intent = this.Intent;           
var action = intent.Action;
var strLink = intent.DataString;
if (Intent.ActionView == action && !string.IsNullOrWhiteSpace(strLink))
{               
//handle intent routing
}
base.OnCreate(savedInstanceState);
}

最新更新