从另一个应用程序控制WPF应用程序
我有一个WPF应用程序,我想从另一个应用程序进行控制。我想要有一些基本的功能,如将焦点设置在特定的控件上,获取控件的文本并将文本/键发送到控件。从另一个应用程序控制WPF应用程序
这可能吗?
UIAutomation就是答案。这是一篇关于CodeProject的有趣文章。
http://www.codeproject.com/Articles/289028/White-An-UI-Automation-tool-for-windows-applicatio
谢谢大家的答案。我曾尝试过Microsoft UI自动化和白色框架。他们两人都很完美。谢谢 – 2013-04-18 11:35:02
是的,这是可能的,也有提供这样做的各种方法。如果它们都在同一个网络上,则可以在它们之间建立TCP连接,都需要一个TCPlistener和一个TCP客户端。
但是,我建议你看看的是WCF。使用WCF,你将能够做到你所需要的(可能还有更多!),但是为了熟悉WCF库,它需要大量的阅读。
对于事物的WCF方面,出格:
您可以通过查看以下启动你的需要做的是:
A.在每个应用程序(在它们的构造函数中)中使用相同的URI作为参考打开一个ServiceHost
。这将打开一个NetNamedPipeBinding
,您可以在这两个应用程序之间进行通信。
实施例:
public static ServiceHost OpenServiceHost<T, U>(T instance, string address)
{
ServiceHost host = new ServiceHost(instance, new Uri[] { new Uri(address) });
ServiceBehaviorAttribute behaviour = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
behaviour.InstanceContextMode = InstanceContextMode.Single;
host.AddServiceEndpoint(typeof(U), new NetNamedPipeBinding(), serviceEnd);
host.Open();
return host;
}
B.创建在相关信道的监听器。这可以在这两个应用程序中完成,以允许双向通信。
实施例:
/// <summary>
/// Method to create a listner on the subscribed channel.
/// </summary>
/// <typeparam name="T">The type of data to be passed.</typeparam>
/// <param name="address">The base address to use for the WCF connection.
/// An example being 'net.pipe://localhost' which will be appended by a service
/// end keyword 'net.pipe://localhost/ServiceEnd'.</param>
public static T AddListnerToServiceHost<T>(string address)
{
ChannelFactory<T> pipeFactory =
new ChannelFactory<T>(new NetNamedPipeBinding(),
new EndpointAddress(String.Format("{0}/{1}",
address,
serviceEnd)));
T pipeProxy = pipeFactory.CreateChannel();
return pipeProxy;
}
C.创建和在这两个应用程序使用,并且在适当的类继承的接口。一些IMyInterface
。
您可以设置一个可以在两个应用程序中使用的库,以允许使用一致的代码库。此类文库将包含这两种方法上述[多]和将在这两个应用中使用,如:
// Setup the WCF pipeline.
public static IMyInterface pipeProxy { get; protected set;}
ServiceHost host = UserCostServiceLibrary.Wcf
.OpenServiceHost<UserCostTsqlPipe, IMyInterface>(
myClassInheritingFromIMyInterface, "net.pipe://localhost/YourAppName");
pipeProxy = UserCostServiceLibrary.Wcf.AddListnerToServiceHost<IMyInterface>("net.pipe://localhost/YourOtherAppName");
pipeProxy
哪里是一些类从IMyInterface
继承。这允许这两个应用程序知道正在传递什么(如果有的话 - 在你的情况下它将是一个无效的,只是一个'提示'让应用程序知道通过接口预先指定的东西)。请注意,我有而不是显示如何调用每个应用程序,你可以自己解决这个问题...
在上面有一些空白,你将不得不填写,但使用我提供的一切应该可以帮助你做你需要的东西。
我希望这会有所帮助。
_是否有可能?_你有没有尝试过任何东西? – 2013-04-09 10:47:54
是的,这是可能的。 – 2013-04-09 10:48:53
请自己尝试一下,然后在SO中提问。 – Luv 2013-04-09 11:04:13