比方说,我想在分配事件处理程序时传递一些额外的数据.请考虑以下代码:
private void setup(string someData)
{
Object.assignHandler(evHandler);
}
public void evHandler(Object sender)
{
// need someData here!!!
}
Run Code Online (Sandbox Code Playgroud)
我如何将someData放入我的evHandler方法?
在下面的代码中,我定义了一个事件处理程序,并希望从中访问age和name变量,而不是全局声明名称和年龄.有没有一种方法,我可以说e.age和e.name?
void Test(string name, string age)
{
Process myProcess = new Process();
myProcess.Exited += new EventHandler(myProcess_Exited);
}
private void myProcess_Exited(object sender, System.EventArgs e)
{
// I want to access username and age here. ////////////////
eventHandled = true;
Console.WriteLine("Process exited");
}
Run Code Online (Sandbox Code Playgroud) Event Handler我使用以下代码订阅/创建自定义:
myButton.Click += (sender, e) => MyButtonClick(sender, e, stuff1, stuff2);
Run Code Online (Sandbox Code Playgroud)
我想取消订阅/删除并尝试如下:
myButton.Click += MyButtonClick;
Run Code Online (Sandbox Code Playgroud)
但抛出以下错误:
No overload for 'MyButtonClick' matches delegate 'System.Windows.RoutedEventHandler'
像这样:
myButton.Click += MyButtonClick(sender, e, stuff1, stuff2);
Run Code Online (Sandbox Code Playgroud)
但抛出以下错误:
Cannot implicitly convert type 'void' to 'System.Windows.RoutedEventHandler'
我如何取消订阅/删除它Event Handler?
我需要将参数(在C#中)传递给事件处理程序,然后才能分离事件处理程序.
我附加事件处理程序并传递参数:
_map.MouseLeftButtonUp += (sender, e) => _map_MouseLeftButtonUp2(sender, e, showResultsWindow);
Run Code Online (Sandbox Code Playgroud)
该事件按预期调用.我尝试分离事件处理程序:
_map.MouseLeftButtonUp -= (sender, e) => _map_MouseLeftButtonUp2(sender, e, showResultsWindow);
Run Code Online (Sandbox Code Playgroud)
代码执行时没有错误,但似乎没有分离.
如果我以更传统的方式附加事件处理程序(不传递参数):
_map.MouseLeftButtonUp+=_map_MouseLeftButtonUp;
Run Code Online (Sandbox Code Playgroud)
和分离
_map.MouseLeftButtonUp -= _map_MouseLeftButtonUp;
Run Code Online (Sandbox Code Playgroud)
一切都按预期工作
通过更传统的方式分离事件处理程序(带参数)
_map.MouseLeftButtonUp -= _map_MouseLeftButtonUp2;
Run Code Online (Sandbox Code Playgroud)
给我一个错误,说代表不匹配(这是有道理的)
所以我的问题是:为什么在传递参数时事件处理程序并没有真正被分离,并且有办法解决这个问题.
使用BizTalk 2013r2 CU1,我为我的入站xsd创建了一个属性模式并部署了该应用程序.
当我使用标准的"xml接收"管道收到样本xml文档时,我可以看到所需的元素按预期被提升到上下文中.
然后,我创建了一个自定义管道,其中包含"Disassemble"阶段中的"XML反汇编程序"组件和"验证"阶段中的自定义组件.此自定义组件需要从上下文中读取提升的属性.但是,我发现当我将接收位置从"xml接收"管道切换到我的自定义管道时,我的属性不会被提升.我在自定义组件中使用以下代码来写出消息上下文中的项列表:
for (int x = 0; x < contextList.CountProperties; x++)
{
contextList.ReadAt(x, out name, out nspace);
string value = contextList.Read(name, nspace).ToString();
contextItems += "Name: " + name + " - " + "Namespace: " + nspace + " - " + value + "\r\n";
if (name == _ContextPropertyName && nspace == _ContextPropertyNamespace)
promotedPropFound = true;
}
Helpers.EventLogHelper eventHelper = new EventLogHelper();
eventHelper.LogEvent(string.Format("Context items:{0}", contextItems));
if (promotedPropFound == false)
throw new Exception(string.Format("Unable to find promoted property with …Run Code Online (Sandbox Code Playgroud) 我阅读了 MSDN 网站和所有内容,但我找不到关于如何引发一个定时事件的简单解释,该事件接受可以是 astring或 的参数double。提供的示例使用ElapsedEventArgs但没有显示出实现我自己对引发事件的论点的好方法。
我的代码(我没有测试过,所以可能是错误的):
private double Pressure_Effect(double p, int t){
time=(double) t;
v=(((p/(rho*y))-g)*time)/(1.0+(mu*time/(rho*y*x)));
return v;
}
private void Time_Handle(){
System.Timers.Timer startTimer=new System.Timers.Timer(70);
startTimer.Elapsed += new ElapsedEventHandler(Activate_Pressure);
}
private void Activate_Pressure(object source, ElapsedEventArgs e){
pressure=2.5;
double v=Pressure_Effect(pressure, 70);
}
Run Code Online (Sandbox Code Playgroud)
我想做的是Activate_Pressure多余的,因为如果我可以直接将事件传递给我Pressure_Effect,但我不知道如何传递。我是 C# 新手,所以请耐心等待。我知道我没有启用计时器,并且此代码中可能缺少其他关键部分,但我只是发布它来澄清我的问题。