MVVM应用程序上接收最简单的Windows消息

MVVM应用程序上接收最简单的Windows消息

本文介绍了如何在UWP XAML MVVM应用程序上接收最简单的Windows消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的大问题:我需要从Windows 10桌面应用程序(或服务,实际上)向同一操作系统实例/机器上的UWP XAML MVVM应用程序发送信号.

My big-picture problem:I need to send a signal from a Windows 10 desktop app (or a service, really) to a UWP XAML MVVM app on the same OS instance / machine.

我在全局名称空间中使用了命名信号量,但是这些信号量在UWP上根本不起作用(出于安全原因,设计使然).没事.

I was using named semaphores in the global namespace, but these don't work at all on UWP (by design, for security reasons maybe). No joy.

我尝试了UWP套接字,并且仅当客户端位于远程计算机上时,它才可以与UWP作为侦听器一起使用.这也是安全设计的决定吗?不知道.将应用程序免于回送限制没有帮助,因为这仅在UWP应用程序是发出请求的客户端时才适用.没事.

I tried UWP sockets, and this works with the UWP as listener only if the client is on a remote machine. Is this a security design decision, too? No idea. Exempting the app from loopback restriction does not help, since that only applies if the UWP app is the client making the request. No joy.

好的,所以我要向操作系统上的特定窗口发送Windows消息...

OK, so I'm left with sending a Windows message to the specific window on the OS...

我的测试应用是GitHub上用于UWP的AdventureWorks示例.我如何处理来自不同进程的Windows消息?我很困惑.

My test app is the AdventureWorks sample for UWP on GitHub. How do I get this to handle a Windows message sent from a different process? I'm stumped.

下面是我的测试客户端代码.

Below is my test client code.

问题:如何在AdventureWorks中处理此消息?将代码更改保持在最低限度很重要,但是目前我很困,并且不知道继续进行的想法. (这似乎是一个秘密的秘密...)

QUESTION:How do I handle this message in AdventureWorks? It's important to keep code changes to a minimum, but currently I'm stuck and have no idea to proceed. (It seems to be a tightly-held secret...)

请帮助!请提供示例代码.

Please help! Sample code please.

class Program
{
    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string lpClassName, String lpWindowName);

    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern uint RegisterWindowMessage(string lpString);

    [DllImport("kernel32.dll")]
    static extern uint GetLastError();

    static void Main(string[] args)
    {
        const string lpClassName = "ApplicationFrameWindow";
        const string lpWindowName = "AdventureWorks.Shopper";
        IntPtr hwnd = FindWindow(lpClassName, lpWindowName);

        uint messageId = RegisterWindowMessage("MetaAutomationMessage");


        int sendMessageResult = SendMessage(hwnd, messageId, IntPtr.Zero, IntPtr.Zero);
        Console.WriteLine(string.Format("Message result is '{0}', ", sendMessageResult));

        uint lastError = GetLastError();
        Console.WriteLine(string.Format("GetLastError result is '{0}', ", lastError));
    }
}

推荐答案

Windows 10 UWP App服务通信可以是在UWP应用之间发送一组键值的选项之一.

Windows 10 UWP App service communication can be one of the options to send set of key-values between UWP apps.

参考: https://msdn.microsoft.com/zh-CN/windows/uwp/launch-resume/how-to-create-and-consume-an-app-service

这篇关于如何在UWP XAML MVVM应用程序上接收最简单的Windows消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 10:47