2013-04-09 154 views
2

我有一個WPF應用程序,我想從另一個應用程序進行控制。我想要有一些基本的功能,如將焦點設置在特定的控件上,獲取控件的文本並將文本/鍵發送到控件。從另一個應用程序控制WPF應用程序

這可能嗎?

+1

_是否有可能?_你有沒有嘗試過任何東西? – 2013-04-09 10:47:54

+1

是的,這是可能的。 – 2013-04-09 10:48:53

+0

請自己嘗試一下,然後在SO中提問。 – Luv 2013-04-09 11:04:13

回答

4

是的,這是可能的,也有提供這樣做的各種方法。如果它們都在同一個網絡上,則可以在它們之間建立TCP連接,都需要一個TCPlistener和一個TCP客戶端。

但是,我建議你看看的是WCF。使用WCF,你將能夠做到你所需要的(可能還有更多!),但是爲了熟悉WCF庫,它需要大量的閱讀。

  1. Efficient communication between two .Net applications

  2. Communication between two winform application using WCF?

  3. Communication between two WPF applications

對於事物的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繼承。這允許這兩個應用程序知道正在傳遞什麼(如果有的話 - 在你的情況下它將是一個無效的,只是一個'提示'讓應用程序知道通過接口預先指定的東西)。請注意,我有而不是顯示如何調用每個應用程序,你可以自己解決這個問題...

在上面有一些空白,你將不得不填寫,但使用我提供的一切應該可以幫助你做你需要的東西。

我希望這會有所幫助。

相關問題