C#- 在“封闭"局部范围内使用变量? [英] C#- using a variable in an 'enclosing' local scope?

查看:167
本文介绍了C#- 在“封闭"局部范围内使用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向 C# 应用程序添加一些新功能 - 特别是尝试复制它的一些行为,但是在 Web 浏览器中,而不是在应用程序中,就像当前那样.

I am trying to add some new features to a C# application- in particular, trying to replicate some of its behavior, but inside a web browser, rather than in the application, as it currently is.

我正在尝试从 MainWindow.cs 类中的方法内部调用已在 Browser.cs 类中定义的方法.

I am trying to call a method that has been defined in the Browser.cs class from inside a method in the MainWindow.cs class.

该方法在 Browser.cs 中定义:

public partial class Browser : Form{
    public Browser(){
        ...
    }
    public void Browser_Load(object sender, EventArgs e){
        webKitBrowser1.Navigate("https://google.com");
    }
    ...
}

然后我尝试从 MainWindow.cs 调用它,如下所示:

I am then trying to call it from MainWindow.cs as follows:

public partial class MainWindow : Window{
    ...
    public MainWindow(){
        ...
        Browser mBrowser = new Browser();
        Object sender = new Object();
        EventArgs e = new EventArgs();
        mBrowser.Browser_Load(sender, e);
        ...
    }
    ...
}

但是,我收到一个编译错误,内容为:

But, I'm getting a compile error that says:

不能在此范围内声明名为 'e' 的局部或参数,因为该名称在封闭的局部范围中用于定义局部或参数

A local or parameter named 'e' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

这是什么意思?我以前从未遇到过这个错误——我在声明它的地方使用相同的范围内的变量——封闭本地范围"是什么意思?那是因为我在括号内使用 e 来调用 mBrowser.Browser_Load(sender, e) 的方法吗?

What does this mean? I've never come across this error before- I am using the variable inside the same scope as where it has been declared- what does it mean by 'enclosing local scope'? Is that because I'm using e inside the parenthesis for the method call to mBrowser.Browser_Load(sender, e)?

当然,由于对这个方法的调用与我定义的 e 在同一个范围内,它不应该是范围的问题吗?

Surely, since the call to this method is inside the same scope as where I've defined e, it shouldn't be an issue of scope?

我确实尝试通过以下方式进行通话:

I did try performing the call with:

mBrowser.Browser_Load(sender, EventArgs e);

但这给了我一个编译错误说:

but this gave me a compile error saying:

'EventArgs' 是一种类型,在给定的上下文中无效.

'EventArgs' is a type, which is not valid in the given context.

谁能指出我在这里做错了什么,我应该怎么做才能正确调用这个方法?

Can anyone point out what I'm doing wrong here, and what I should be doing to be able to call this method correctly?

推荐答案

错误很明显,你已经在你的作用域中定义了 e 命名变量,(可能在您尚未显示的代码).

The error is pretty clear, you have already defined e named variable in your scope, (Probably in the part of code that you haven't shown).

但更重要的是,您不应该像那样调用 Load 事件,而是在单独的方法中提取功能并从您的 中调用该方法加载事件等.

But more importantly, you shouldn't be calling the Load event like that, instead extract the functionality in a separate method and call the method from your Load event and other places.

喜欢:

public void SomeMethodToBeCalledOnLoad()
{
    webKitBrowser1.Navigate("https://google.com");
}

public void Browser_Load(object sender, EventArgs e)
{
    SomeMethodToBeCalledOnLoad();
}

public MainWindow(){
    ...
    Browser mBrowser = new Browser();
    Object sender = new Object();
    EventArgs e = new EventArgs();
    SomeMethodToBeCalledOnLoad();//here
    ...
}

这篇关于C#- 在“封闭"局部范围内使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆