如何通过URI激活打开Winui Maui?
我创建了一个毛伊岛Windows应用程序。我希望通过URI激活应用程序,然后将查询参数传递给应用程序。
我添加了Windows协议,用于通过包装中的URI调用该应用程序:
<Extensions>
<uap:Extension Category="windows.protocol">
<uap:Protocol Name="my-app">
<uap:DisplayName>My App</uap:DisplayName>
</uap:Protocol>
</uap:Extension>
</Extensions>
当我通过浏览器my-app://foo.com?user = 123456激活应用程序时,应用程序启动,但它会在冷启动下启动。在我的Win UI应用程序中,我已经覆盖了OnLaunched方法,但是无论我如何启动该应用程序,我都无法访问该协议。我正在尝试从UWP应用程序中重新创建以下代码:
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.Protocol)
{
ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
var queryStr = eventArgs.Uri.Query;
App.UserId = System.Web.HttpUtility.ParseQueryString(queryStr).Get("user");
// Navigate to a view
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
Xamarin.Forms.Forms.Init(args);
Window.Current.Content = rootFrame;
}
rootFrame.Navigate(typeof(MainPage), eventArgs);
}
Window.Current.Activate();
}
因此,我从此开始,但是UwplaunchactivatedEventarg一直以启动而不是协议返回。
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
var kind = args.UWPLaunchActivatedEventArgs.Kind;
base.OnLaunched(e)
}
I've created a Maui windows application. I'm looking to activate the application through a URI and pass query parameters to the app.
I've added the windows protocol for calling the app via uri in the package manifest:
<Extensions>
<uap:Extension Category="windows.protocol">
<uap:Protocol Name="my-app">
<uap:DisplayName>My App</uap:DisplayName>
</uap:Protocol>
</uap:Extension>
</Extensions>
When I activate the application via the browser my-app://foo.com?user=123456 the app launches, but it launches as a cold start. Within my Win UI app I've overrode the onLaunched method, but regardless of how I've launched the app I cannot get access to the protocol. I'm trying to recreate the following code from my UWP Application:
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.Protocol)
{
ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
var queryStr = eventArgs.Uri.Query;
App.UserId = System.Web.HttpUtility.ParseQueryString(queryStr).Get("user");
// Navigate to a view
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
Xamarin.Forms.Forms.Init(args);
Window.Current.Content = rootFrame;
}
rootFrame.Navigate(typeof(MainPage), eventArgs);
}
Window.Current.Activate();
}
So I've started with this, but UWPLaunchActivatedEventArg is consistently being returned as Launch instead of protocol.
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
var kind = args.UWPLaunchActivatedEventArgs.Kind;
base.OnLaunched(e)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我的解决方案:
使用参数运行URI,这是屏幕截图:

Here is my solution:
run your URI with parameters and here is the screenshot:

我花了一段时间才能理解其他答案的含义 - 要“铸造”对象。
这给我带来了URI字符串。但是 - 我得到了一个新窗口。我想将URI派遣到已经创建的窗口(如果存在,否则可以创建)。
我已经通过捕获狂热的事件与Xamarin.forms UWP完成了。还没有想到毛伊岛。
鲍勃
It took me a while to understand what the other answer meant -- got to "cast" the object.
This gets me the URI string. BUT -- I get a new window created. I want to dispatch the URI to an already created window (if it exists, otherwise create).
I've done with with Xamarin.Forms UWP, by catching the OnActivated event. Haven't got that figured for MAUI yet.
Bob
好的,最后我找到了一个有用的文档来解决这个问题。我遇到了这些问题:
我无法通过协议进行任何激活或肿瘤的事件调用。
改为打开一个新窗口。
旧实例(例如,登录窗口在等待)未收到任何信号,表明
my-app:// login/result = xyz
已被调用。我发现应用程序生命周期功能迁移,我建议阅读它,至少我链接的部分,它将帮助您了解问题以及为什么我以这种方式解决它。请注意,尽管链接到单个实例应用程序,但我只使用它来解决问题,而解决方案并不迫使您的应用程序成为单一结构。
总而言之,Windows App SDK的流量(基于Winui3基于Winui3,又基于MAUI)更改了:
my-app:// url
my-app:// url
协议
3。Applaight( instance2 )的接收 在Windows App SDK中,您以前的
实例1
实例没有激活信号。要解决这个问题,我们使用 redirectactivation toasync :
onlaunch onlaunch
中,是否in
是 procest 。这只会发生在实例2
(URI启动的实例)中。base.onlaunched
在此方法中,因此没有创建新窗口。协议
的启动,请登记激活事件,以收听由instance2
转发的协议事件。 注意毛伊岛:您还必须为毛伊岛这样做。由于某些原因,anc活化
生命周期未在此处调用。这是解决方案,它适用于MAUI,但我相信它也适用于Winui3和Windows App SDK:
更新:对于毛伊岛,如果您不想触摸
app.xaml.cs ,您可以使用
builder.configurelifecycleevents
:修改
app.xaml.cs
的旧代码:Okay finally I found a useful doc to solve this problem. I faced these issues:
I couldn't get any Activated or OnActivated event call with the Protocol.
A new window is opened instead.
The old instance (where the login window is waiting for example) does not receive any signals that a URI like
my-app://login/result=xyz
has been called.I found out Application lifecycle functionality migration, I recommend reading it, at least the section I linked, it will help you understand the issue and why I solved it this way. Note that despite linking to a Single Instance Apps, I only use it to solve the issue and the solution does not force your app to become a single-instance.
To summarize the article, the flow of Windows App SDK (which WinUI3 based on, which in turn MAUI based on) changed:
my-app://url
my-app://url
OnActivated
withProtocol
Protocol
As you can see, in Windows App SDK, no Activated signal is given to your former
Instance1
instance.To fix that, we use RedirectActivationToAsync:
OnLaunch
, check if theKind
isProtocol
. This would only happen toInstance2
(the instances that are launched by URI).base.OnLaunched
in this method so no new window is created.Protocol
, registerActivated
event to listen to Protocol event forwarded byInstance2
. NOTE for MAUI: you also have to do this for MAUI. For some reason theOnActivated
lifecycle is not called here.Here's the solution, it's for MAUI but I believe it applies to WinUI3 and Windows App SDK as well:
Update: for MAUI, if you do not want to touch
App.xaml.cs
, you can use thebuilder.ConfigureLifecycleEvents
:Old code that modify
App.xaml.cs
:我添加了这个新的,更完整的答案,尽管我还没有假装完全理解这一点,而且我并没有做出很好的工作跟踪我从哪里获得的示例代码。而且,我应该几个月前回到这里。
请注意,将我的logp()方法写入控制台,而mainpage.inst.setopeningstring()则通过URI。
Windows/program.cs:
I'm adding this new, more complete answer, though I don't pretend to completely understand this yet, and I did not do great job keeping track of what sample code I got from where. And, I should've gotten back to here months ago.
Note my logp() method which writes to the console, and MainPage.Inst.setOpeningString() which passes the URI along.
Windows/Program.cs: