简体   繁体   English

如何在启动后检测 WPF 桌面 package (WAP) 上的 URI 激活?

[英]How to detect URI activation on a WPF desktop package (WAP) after it has already launched?

I have a WPF desktop application that is MSIX-packaged using a Windows Application Package (WAP) project.我有一个 WPF 桌面应用程序,它使用 Windows 应用程序 Package (WAP) 项目进行 MSIX 打包。 I already know how to launch my WPF desktop app the first time using URI activation, by calling AppInstance.GetActivatedEventArgs() and then analyzing the arguments:我已经知道如何在第一次使用 URI 激活时启动我的 WPF 桌面应用程序,方法是调用AppInstance.GetActivatedEventArgs()然后分析 arguments:

if (activatedEventArgs.Kind == ActivationKind.Launch)
{
    if (((LaunchActivatedEventArgs)activatedEventArgs).Arguments == "myactivationcode")
        // .. do something
}

But if a user runs the URI activation a 2nd time, while my app is already launched, I have learned that a new instance of my app is launched.但是如果用户第二次运行 URI 激活,而我的应用程序已经启动,我了解到我的应用程序的一个新实例已启动。 This doesn't happen with UWP apps, just desktop apps. UWP 应用程序不会发生这种情况,只是桌面应用程序。 I can kill the 2nd instance to follow a desired singleton pattern, but what I want is for the first instance of my WPF app to get some event that lets it know to come back into view.我可以杀死第二个实例以遵循所需的 singleton 模式,但我想要的是让我的 WPF 应用程序的第一个实例获得一些事件,让它知道重新进入视图。

Things I've researched that have no answers that I can see:我研究过的事情没有我能看到的答案:

  1. How to handle URI activation in a Windows Application Packaging Project? Windows应用打包工程URI激活如何处理?
  2. How can I handle file activation from a WPF app which is running as UWP? 如何处理来自运行为 UWP 的 WPF 应用程序的文件激活?
  3. https://learn.microsoft.com/en-us/windows/uwp/launch-resume/handle-uri-activation#step-3-handle-the-activated-event https://learn.microsoft.com/en-us/windows/uwp/launch-resume/handle-uri-activation#step-3-handle-the-activated-event

Does any such API or event exist for URI re-activation? URI 重新激活是否存在任何此类 API 或事件? Or do I need to do some other form of IPC, like named pipes or WCF on the 2nd instance of my app?或者我是否需要在我的应用程序的第二个实例上执行其他形式的 IPC,例如命名管道或 WCF? Any help here would be greatly appreciated.这里的任何帮助将不胜感激。

But if a user runs the URI activation a 2nd time, while my app is already launched, I have learned that a new instance of my app is launched.但是如果用户第二次运行 URI 激活,而我的应用程序已经启动,我了解到我的应用程序的一个新实例已启动。

Whether a second instance is launched depends on the implementation of your custom Main method.是否启动第二个实例取决于自定义Main方法的实现。

In your second link , there is a link to blog post and a code example that demonstrates how to prevent another instance from being launched.在您的第二个链接中,有一个指向博客文章的链接和一个演示如何防止启动另一个实例的代码示例

It uses named pipes to communicate with the already running app instance and passes a serialized IActivatedEventArgs to it:它使用命名管道与已经运行的应用程序实例通信,并将序列化的IActivatedEventArgs传递给它:

[STAThread]
static void Main(string[] args)
{
    IActivatedEventArgs activatedEventArgs = AppInstance.GetActivatedEventArgs();
    using (Mutex mutex = new Mutex(false, AppUniqueGuid))
    {
        if (mutex.WaitOne(0, false))
        {
            new Thread(CreateNamedPipeServer) { IsBackground = true }
                .Start();

            s_application = new App();
            s_application.InitializeComponent();
            if (activatedEventArgs != null)
                s_application.OnProtocolActivated(activatedEventArgs);
            s_application.Run();
        }
        else if (activatedEventArgs != null)
        {
            //instance already running
            using (NamedPipeClientStream namedPipeClientStream
                = new NamedPipeClientStream(NamedPipeServerName, AppUniqueGuid, PipeDirection.Out))
            {
                try
                {
                    namedPipeClientStream.Connect(s_connectionTimeout);
                    SerializableActivatedEventArgs serializableActivatedEventArgs = Serializer.Serialize(activatedEventArgs);
                    s_formatter.Serialize(namedPipeClientStream, serializableActivatedEventArgs);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, string.Empty, MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
    }
}

Does any such API or event exist for URI re-activation? URI 重新激活是否存在任何此类 API 或事件?

No

Or do I need to do some other form of IPC, like named pipes or WCF on the 2nd instance of my app?或者我是否需要在我的应用程序的第二个实例上执行其他形式的 IPC,例如命名管道或 WCF?

Yes.是的。 Again, please refer to the mentioned blog post and accompanied code sample.同样,请参阅提到的博客文章和随附的代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM