将类添加到当前事件处理程序

时间:2018-01-27 07:21:31

标签: c# winforms

我想将类添加到当前事件处理程序。

像这样的原始事件参数;

void ugTopActivity_ChartDataClicked(object sender, ChartDataEventArgs e)

我想添加另一个类,如FillSceneGraphEventArgs;

private void ugTopActivity_ChartDataClicked(object sender, ChartDataEventArgs e, FillSceneGraphEventArgs ex)
{
    box = new Box(test);
    ex.SceneGraph.Add(box);
}

然而它会抛出一个错误;

  

'匹配委托'

没有重载

我该如何通过?

修改-1

private void ugTopActivity_ChartDataClicked(object sender, ChartDataEventArgs e)
{            
    var o = sender as FillSceneGraphEventArgs;   --> 'Object not set instance of an object' 

    IAdvanceAxis x = (IAdvanceAxis)o.Grid["X"];

    IAdvanceAxis y = (IAdvanceAxis)o.Grid["Y"];

    Box box = new Box(new Infragistics.Win.DataVisualization.Rectangle((double)x.Map(0), (double)y.Map(1), 50, 371));
    box.PE.Fill = Color.Black;
    box.PE.FillOpacity = 70;

    if (o != null)
    {
        o.SceneGraph.Add(box);
    }
}

3 个答案:

答案 0 :(得分:2)

事件处理程序具有已定义的参数计数。

标准地说,当您需要添加新数据/'返回值'时,它会被添加到参数类中,在本例中为ChartDataEventArgs类。

您无法添加新参数,因为调用者无法调用参数多于预期的方法。它始终使用' object sender'和'论证'。下一个参数应该有什么用?

当你是调用者的实现者时,你可以改变它,但最好的方法是更新参数类。

答案 1 :(得分:2)

正确的方法

目标方法是使用EventArgs参数传递包含所需信息的类。因此,在这种情况下,您必须扩展ChartDataEventArgs,或者可能提供自己的继承自EventArgs的类。

偷偷摸摸的方式

另一方面,如果您需要公开某些信息,但不是每个事件都会发生变化,您也可以将其公开为引发事件的类的公共属性。然后你可以通过强制转发发件人来访问它。您的示例并没有告诉我发件人的类型,但是说它是MyChartClass,您可以这样做:

class MyChartClass
{
    public FillSceneGraph GetFillSceneGraph()
    {
        get 
        {
            return _somethingOrOther;
        }
    }
}

然后像这样更新处理程序:

private void ugTopActivity_ChartDataClicked(object sender, ChartDataEventArgs e)
{
    var o = sender as MyChartClass; //Or whatever type it is
    if (o != null) 
    {
        var box = new Box(test);
        var sceneGraph = o.GetFillSceneGraph();  //Get the scene graph from the sender
        sceneGraph.Add(box);  //Add the box to the graph
    }
}

答案 2 :(得分:1)

如果您需要其他参数,请先转到您的活动声明,然后再使用lambda。它应该是这样的:

org.openqa.selenium.WebDriverException: unknown error: cannot determine loading status
from unknown error: missing or invalid 'entry.level'
  (Session info: chrome=63.0.3239.132)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.5.3', revision: 'a88d25fe6b', time: '2017-08-29T12:42:44.417Z'
System info: host: 'BADRI-HP', ip: '---.---.-.---', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\Badri\AppData\Local\Temp\scoped_dir45280_30736}, takesHeapSnapshot=true, pageLoadStrategy=normal, unhandledPromptBehavior=, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=63.0.3239.132, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, platformName=XP, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 60b98ca106d00b50963c47e02276c40b
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:82)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:45)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:646)
    at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:370)
    at naukri.ProfileUpdate.login(ProfileUpdate.java:19)
    at testScripts.NewTest.f(NewTest.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)