C# 从另一个对象访问表单组件时抛出;System.NullReferenceException未经处理";
我试图从WCF对象中修改属于Form2的文本框C# 从另一个对象访问表单组件时抛出;System.NullReferenceException未经处理";,c#,C#,我试图从WCF对象中修改属于Form2的文本框 namespace server2 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private ServiceHost duplex; private void Form2_Load(object sender,
namespace server2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private ServiceHost duplex;
private void Form2_Load(object sender, EventArgs e) /// once the form loads, create and open a new ServiceEndpoint.
{
duplex = new ServiceHost(typeof(ServerClass));
duplex.AddServiceEndpoint(typeof(IfaceClient2Server), new NetTcpBinding(), "net.tcp://localhost:9080/service");
duplex.Open();
this.Text = "SERVER *on-line*";
}
}
class ServerClass : IfaceClient2Server
{
IfaceServer2Client callback;
public ServerClass()
{
callback = OperationContext.Current.GetCallbackChannel<IfaceServer2Client>();
}
public void StartConnection(string name)
{
var myForm = Form.ActiveForm as Form2;
myForm.textBox1.Text = "Hello world!"; /// <- this one trows “System.NullReferenceException was unhandled”
/// unless Form2 is selected when this fires.
callback.Message_Server2Client("Welcome, " + name );
}
public void Message_Cleint2Server(string msg)
{
}
public void Message2Client(string msg)
{
}
}
[ServiceContract(Namespace = "server", CallbackContract = typeof(IfaceServer2Client), SessionMode = SessionMode.Required)]
public interface IfaceClient2Server ///// what comes from the client to the server.
{
[OperationContract(IsOneWay = true)]
void StartConnection(string clientName);
[OperationContract(IsOneWay = true)]
void Message_Cleint2Server(string msg);
}
public interface IfaceServer2Client ///// what goes from the sertver, to the client.
{
[OperationContract(IsOneWay = true)]
void AcceptConnection();
[OperationContract(IsOneWay = true)]
void RejectConnection();
[OperationContract(IsOneWay = true)]
void Message_Server2Client(string msg);
}
}
命名空间服务器2
{
公共部分类表单2:表单
{
公共表格2()
{
初始化组件();
}
专用服务主机双工;
private void Form2_Load(object sender,EventArgs e)///表单加载后,创建并打开新的ServiceEndpoint。
{
duplex=新的ServiceHost(typeof(ServerClass));
duplex.AddServiceEndpoint(typeof(IfaceClient2Server)),新的NetTcpBinding(),“net。tcp://localhost:9080/service");
duplex.Open();
this.Text=“服务器*在线*”;
}
}
类服务器类:IfaceClient2Server
{
IfaceServer2Client回调;
公共服务器类()
{
callback=OperationContext.Current.GetCallbackChannel();
}
public void StartConnection(字符串名称)
{
var myForm=Form.ActiveForm作为Form2;
myForm.textBox1.Text=“你好,世界!”;//myForm
可能不是Form2
类型,或者可能不包含textBox1
字段。请确保为这两种情况检查null。myForm
可能不是Form2
类型,或者可能不包含textBox1
字段。请确保为这两种情况检查nulle案例。正如在初始问题的注释中所讨论的,问题是当您想要的表单未处于活动状态时,您引用的是ActiveForm。每当尝试使用As
关键字强制转换时,如果强制转换无效,则结果将为空。因为您获取了无法强制转换为Form2的表单(因为它是另一种形式),您正确地收到了空引用异常
假设您已经在Form2上实施了单例规则,并且没有使用表单的名称,您可以通过集合的方式访问它,如下所示:
(Form2)Application.OpenForms["Form2"];
在您的代码示例中,可能如下所示:
public void StartConnection(string name)
{
//var myForm = Form.ActiveForm as Form2;
var myForm = Application.OpenForms["Form2"] as Form2;
myForm.textBox1.Text = "Hello world!"; /// <- this one trows “System.NullReferenceException was unhandled”
/// unless Form2 is selected when this fires.
callback.Message_Server2Client("Welcome, " + name );
}
public void StartConnection(字符串名称)
{
//var myForm=Form.ActiveForm作为Form2;
var myForm=Application.OpenForms[“Form2”]作为Form2;
myForm.textBox1.Text=“你好,世界!”///正如在初始问题的注释中所讨论的,问题是当您想要的表单未处于活动状态时,您正在引用ActiveForm。每当尝试使用As
关键字强制转换时,如果强制转换无效,则结果将为空。因为您获取了无法强制转换为Form2的表单(因为它是另一种形式),您正确地收到了空引用异常
假设您已经在Form2上实施了单例规则,并且没有使用表单的名称,您可以通过集合的方式访问它,如下所示:
(Form2)Application.OpenForms["Form2"];
在您的代码示例中,可能如下所示:
public void StartConnection(string name)
{
//var myForm = Form.ActiveForm as Form2;
var myForm = Application.OpenForms["Form2"] as Form2;
myForm.textBox1.Text = "Hello world!"; /// <- this one trows “System.NullReferenceException was unhandled”
/// unless Form2 is selected when this fires.
callback.Message_Server2Client("Welcome, " + name );
}
public void StartConnection(字符串名称)
{
//var myForm=Form.ActiveForm作为Form2;
var myForm=Application.OpenForms[“Form2”]作为Form2;
myForm.textBox1.Text=“Hello world!”;///您确定活动表单实际上是Form2
的实例吗?如果不是,那么as
将返回null
。不,实际上不是!我应该如何访问它们?我们需要更多关于表单设置的信息,以便准确地找到您的表单实例的位置“表格2"生命…如果它不是Form2实例,为什么要访问它?显然,你希望它是一个带有特定文本框的Form2实例。你需要用英语告诉我们你想做什么。textBox1是Form2实例。但我不是从另一个实例访问它,而是从一个wcf对象访问它。你确定活动表单实际上是一个实例吗Form2的ce
?如果不是,那么as
将返回null
。不,实际上不是!我应该如何访问它们?我们需要更多关于表单如何设置的信息,以便准确找出“Form2”实例的位置生命…如果它不是一个FrO2实例,你为什么要访问它?很明显,你希望它是一个带有它上特定文本框的FrO2实例。你需要用英语告诉我们你想要什么。TrimBox是一个FrO2实例。但是我不是从另一个,而是一个WCF对象访问它。很高兴听到它。不要忘记考虑我的最后一个。很高兴听到这句话。别忘了考虑我的最后一句话。