通过点击事件处理程序传递字符串

huangapple go评论61阅读模式
英文:

Pass Strings through click EventHandler

问题

我正在尝试将3个字符串发送到一个void/EventHandler。

button.Click += new DownloadGame(gameZip, gameExe, gameTitle);

private void DownloadGame(string gameZip, string gameExe, string gameTitle)
{
    if (!File.Exists(gameExe))
    {
        MessageBox.Show("Already Installed!");
    }
    string GamesDirectory = Path.Combine(Environment.CurrentDirectory, "Games");
    if (!Directory.Exists(GamesDirectory))
        Directory.CreateDirectory(GamesDirectory);
    InstallGameFiles(Path.Combine(Directory.GetCurrentDirectory(), "Build", gameExe), gameZip, gameTitle);
}

如何在不出现此错误的情况下调用带有参数的方法?

错误 CS0246:找不到类型或命名空间名为 'DownloadGame'(是否缺少 using 指令或程序集引用?)

英文:

I'm trying to send 3 strings to a void/EventHandler

 button.Click += new DownloadGame(gameZip, gameExe, gameTitle);

private void DownloadGame(string gameZip, string gameExe, string gameTitle)
        {
            if (!File.Exists(gameExe))
            {
                MessageBox.Show("Already Installed!");
            }
            string GamesDirectory = Path.Combine(Environment.CurrentDirectory, "Games");
            if (!Directory.Exists(GamesDirectory))
                Directory.CreateDirectory(GamesDirectory);
            InstallGameFiles(Path.Combine(Directory.GetCurrentDirectory(), "Build", gameExe), gameZip, gameTitle);

How can I call the method with the arguments without this error?

> Error CS0246 The type or namespace name 'DownloadGame' could not be found (are you missing a using directive or an assembly reference?)

答案1

得分: 0

你不能将字符串传递给Click事件。但是你可以在该按钮所属的类下创建变量/属性。
此外,当你将函数添加到事件时,不能使用'new'关键字。

string gameZip, gameExe, gameTitle; // 在这里设置这些变量

button.Click += DownloadGame;

private void DownloadGame(Object sender, EventArgs e)
{
    if (!File.Exists(gameExe))
    {
        MessageBox.Show("已安装!");
    }
    string GamesDirectory = Path.Combine(Environment.CurrentDirectory, "Games");
    if (!Directory.Exists(GamesDirectory))
        Directory.CreateDirectory(GamesDirectory);
    InstallGameFiles(Path.Combine(Directory.GetCurrentDirectory(), "Build", gameExe), gameZip, gameTitle);   
}
英文:

You cannot pass string to Click event. But you can create variable/property under class where this button belong.
Also, when you are adding function to Event, you can't use 'new' keyword.

string gameZip, gameExe, gameTitle; // Set those variables i.e. here

button.Click += DownloadGame;

private void DownloadGame(Object sender, EventArgs e)
{
    if (!File.Exists(gameExe))
    {
        MessageBox.Show("Already Installed!");
    }
    string GamesDirectory = Path.Combine(Environment.CurrentDirectory, "Games");
    if (!Directory.Exists(GamesDirectory))
        Directory.CreateDirectory(GamesDirectory);
    InstallGameFiles(Path.Combine(Directory.GetCurrentDirectory(), "Build", gameExe), gameZip, gameTitle);   
}

答案2

得分: 0

你得到错误 CS0246的原因是new关键字:

// 错误的写法
button.Click += new DownloadGame(gameZip, gameExe, gameTitle);

由于使用了new关键字,编译器试图创建一个名为DownloadGameclass的新实例。但是它找不到这个名字的类(只有一个同名的方法),因此出现了错误。

另一个语法错误的原因是委托的签名必须匹配。例如,Winforms按钮处理程序应该这样赋值:

public MainForm()
{
    buttonDownload.Click += onClickDownloadButton;
}

private void onClickDownloadButton(object sender, EventArgs e)
{
}

无论平台如何,一旦输入button.Click += ,智能感知应该会提供一个具有正确签名的方法。

至于你的代码尝试做什么,处理Click事件是为了接收Button类通知的事件和数据,而不是发送字符串或其他内容。

在这里似乎有一个合理的假设,即你已经执行了其他UI操作来填充这些文件名。例如,你可能有一个按钮或菜单来调用“打开文件”流程,或者一个设置提示框。

private void onClickDownloadButton(object sender, EventArgs e)
{
    DownloadGame();
}

public void DownloadGame()
{
    if (!File.Exists(gameExe))
    {
        MessageBox.Show("Already Installed!");
    }
    string GamesDirectory = Path.Combine(Environment.CurrentDirectory, "Games");
    if (!Directory.Exists(GamesDirectory))
    {
        Directory.CreateDirectory(GamesDirectory);
    }
    InstallGameFiles(Path.Combine(Directory.GetCurrentDirectory(), "Build", gameExe), gameZip, gameTitle);
}

// 这些变量需要通过其他方式填充
private string gameZip;
private string gameExe;
private string gameTitle;

请在启用buttonDownload之前以其他方式填充这些变量。

英文:

The reason you're getting Error CS0246. is the new keyword:

// I N C O R R E C T
button.Click += new DownloadGame(gameZip, gameExe, gameTitle);

Because of the new, the compiler is attempting to make a new instance of a class named DownloadGame. It can't find a class by that name because it doesn't exist (there is only a method by that name, thus the error).


The other reason that your syntax is incorrect is that the delegate signature must match. For example, a Winforms button handler would assign like this:

public MainForm()
{
    buttonDownload.Click += onClickDownloadButton;
}
private void onClickDownloadButton(object sender, EventArgs e)
{
}

Regardless of the platform, once you type button.Click += the intellisense should offer to make a method with the correct signature.

And as far as what your code is attempting to do, handling the Click event is to receive the event and data that the Button class is notifying and is not intended to send strings or anything else.


The thing that would seem to make sense here is that you have already performed other UI actions to populate some of these file names. For example you may have a button or menu that invokes an "open file" flow, or a settings prompt (example).

private void onClickDownloadButton(object sender, EventArgs e)
{
    DownloadGame();
}
public void DownloadGame()
{
    if (!File.Exists(gameExe))
    {
        MessageBox.Show("Already Installed!");
    }
    string GamesDirectory = Path.Combine(Environment.CurrentDirectory, "Games");
    if (!Directory.Exists(GamesDirectory))
    {
        Directory.CreateDirectory(GamesDirectory);
    }
    InstallGameFiles(Path.Combine(Directory.GetCurrentDirectory(), "Build", gameExe), gameZip, gameTitle);
}
// These need to be populated some other 
// way before enabling buttonDownload.    
private string gameZip;
private string gameExe;
private string gameTitle;

huangapple
  • 本文由 发表于 2023年1月8日 21:54:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定