WCF,访问窗口从服务窗体控件 [英] WCF, Accessing a windows forms controls from a service
本文介绍了WCF,访问窗口从服务窗体控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Windows窗体承载的WCF服务。
我如何访问形式的控制从方法,我的服务?
例如我有
公共接口IService {
[服务合同]
字符串PrintMessage(字符串消息);
}公共类服务:IService
{
公共字符串PrintMessage(字符串消息)
{
//我如何访问从这里窗体控件?
FormTextBox.Text =消息;
}
}
解决方案
首先,ServiceContract特性应该是在界面上,而不是PrintMessage()方法。
用你的例子的修正版本,你可以这样来做。
[的ServiceContract]
公共接口IService
{
[OperationContract的]
字符串PrintMessage(字符串消息);
}
公共类服务:IService
{
公共字符串PrintMessage(字符串消息)
{
//这里调用委托。
尝试{
UpdateTextDelegate处理器= TextUpdater;
如果(处理!= NULL)
{
处理器(这一点,新UpdateTextEventArgs(消息));
}
} {抓
}
}
公共静态UpdateTextDelegate TextUpdater {搞定;组; }
}公共委托无效UpdateTextDelegate(对象发件人,UpdateTextEventArgs E);公共类UpdateTextEventArgs
{
公共字符串文本{搞定;组; }
公共UpdateTextEventArgs(字符串文本)
{
文本=文本;
}
}公共类的MainForm:表格
{
公众的MainForm()
{
的InitializeComponent(); //此处更新您的服务代表。
Service.TextUpdater = ShowMessageBox; //这里创建WCF服务
ServiceHost的为myService =新的ServiceHost(typeof运算(IService),URI);
}
//该ShowMessageBox()方法来匹配的签名
//将UpdateTextDelegate委托。
公共无效ShowMessageBox(对象发件人,UpdateTextEventArgs E)
{
//使用的invoke(),以确保UI交互发生
//在UI线程上......万一这个委托是
//调用另一个线程。
调用((MethodInvoker)代表{
MessageBox.Show(e.Text);
});
}
}
这实质上是由@Simon福克斯提出的解决方案,即使用委托。这希望只是把骨头上的肉一些,可以这么说。
I have a WCF service that is hosted inside a Windows Form.
How can I access the controls of the form from the methods in my service?
for example I have
public interface IService {
[ServiceContract]
string PrintMessage(string message);
}
public class Service: IService
{
public string PrintMessage(string message)
{
//How do I access the forms controls from here?
FormTextBox.Text = message;
}
}
解决方案
First of all, the ServiceContract attribute should be on the interface, not the PrintMessage() method.
Using a corrected version of your example, you can do it this way.
[ServiceContract]
public interface IService
{
[OperationContract]
string PrintMessage(string message);
}
public class Service : IService
{
public string PrintMessage(string message)
{
// Invoke the delegate here.
try {
UpdateTextDelegate handler = TextUpdater;
if (handler != null)
{
handler(this, new UpdateTextEventArgs(message));
}
} catch {
}
}
public static UpdateTextDelegate TextUpdater { get; set; }
}
public delegate void UpdateTextDelegate(object sender, UpdateTextEventArgs e);
public class UpdateTextEventArgs
{
public string Text { get; set; }
public UpdateTextEventArgs(string text)
{
Text = text;
}
}
public class MainForm : Form
{
public MainForm()
{
InitializeComponent();
// Update the delegate of your service here.
Service.TextUpdater = ShowMessageBox;
// Create your WCF service here
ServiceHost myService = new ServiceHost(typeof(IService), uri);
}
// The ShowMessageBox() method has to match the signature of
// the UpdateTextDelegate delegate.
public void ShowMessageBox(object sender, UpdateTextEventArgs e)
{
// Use Invoke() to make sure the UI interaction happens
// on the UI thread...just in case this delegate is
// invoked on another thread.
Invoke((MethodInvoker) delegate {
MessageBox.Show(e.Text);
} );
}
}
This is essentially the solution suggested by @Simon Fox, i.e., use a delegate. This hopefully just puts some flesh on the bones, so to speak.
这篇关于WCF,访问窗口从服务窗体控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文