wpf C#- 在“封闭”局部范围内使用变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36893000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#- using a variable in an 'enclosing' local scope?
提问by Noble-Surfer
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.
我正在尝试向 C# 应用程序添加一些新功能 - 特别是,尝试复制它的一些行为,但是在 Web 浏览器中,而不是在应用程序中,就像现在一样。
I am trying to call a method that has been defined in the Browser.csclass from inside a method in the MainWindow.csclass.
我试图Browser.cs从类中的方法内部调用已在类中定义的方法MainWindow.cs。
The method is defined in Browser.cswith:
该方法定义Browser.cs为:
public partial class Browser : Form{
public Browser(){
...
}
public void Browser_Load(object sender, EventArgs e){
webKitBrowser1.Navigate("https://google.com");
}
...
}
I am then trying to call it from MainWindow.csas follows:
然后我尝试从MainWindow.cs以下方式调用它:
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:
但是,我收到一个编译错误,内容为:
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”的本地或参数,因为该名称用于封闭本地范围以定义本地或参数
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 einside the parenthesis for the method call to mBrowser.Browser_Load(sender, e)?
这是什么意思?我以前从来没有遇到过这个错误——我在声明它的地方使用同一个范围内的变量——“封闭本地范围”是什么意思?那是因为我e在括号内使用了方法调用mBrowser.Browser_Load(sender, 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?
当然,由于对此方法的调用与我定义的范围在同一范围内e,所以它不应该是范围问题?
I did try performing the call with:
我确实尝试通过以下方式执行呼叫:
mBrowser.Browser_Load(sender, EventArgs e);
but this gave me a compile error saying:
但这给了我一个编译错误说:
'EventArgs' is a type, which is not valid in the given context.
'EventArgs' 是一种类型,在给定的上下文中无效。
Can anyone point out what I'm doing wrong here, and what I should be doing to be able to call this method correctly?
谁能指出我在这里做错了什么,我应该怎么做才能正确调用这个方法?
回答by Habib
The error is pretty clear, you have already defined enamed variable in your scope, (Probably in the part of code that you haven't shown).
错误很明显,你已经e在你的范围内定义了命名变量,(可能在你没有显示的代码部分)。
But more importantly, you shouldn't be calling the Loadevent like that, instead extract the functionality in a separate method and call the methodfrom your Loadevent and other places.
但更重要的是,您不应该Load像那样调用事件,而是在单独的方法中提取功能并从您的Load事件和其他地方调用该方法。
Like:
喜欢:
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
...
}
回答by Juan M. Elosegui
You already have a variable named ein the scope.
您已经有一个e在作用域中命名的变量。
try to call your method like this
尝试像这样调用你的方法
mBrowser.Browser_Load(this, EventArgs.Empty);
mBrowser.Browser_Load(this, EventArgs.Empty);
and the error should go.
错误应该消失。

