我试图在Winforms中创建下面代码的函数。您看到的是一个名为ytplayerSearch的函数,而不是ytplayer。目前的代码工作,但我一直试图使它更干净。
private void btPlayMin_Click(object sender, EventArgs e)
{
ytplayer.playLink(sender, e, link);
ytplayer.miniMax("Normal", new Size(300, 24), new Size(400, 95), false, "", FormBorderStyle.FixedToolWindow, true);
ytplayer.TopMost = true;
ytplayer.BringToFront();
ytplayer.TopMost = false;
int x = Screen.PrimaryScreen.WorkingArea.Width - 375;
ytplayer.Location = new Point(x, 0);
ytplayer.Show();
this.Close();
}
private void btPlayNorm_Click(object sender, EventArgs e)
{
ytplayer.playLink(sender, e, link);
ytplayer.miniMax("Minimal", new Size(300, 240), new Size(400,335), true, "YoutubePlayer", FormBorderStyle.FixedSingle, false);
ytplayer.TopMost = true;
ytplayer.BringToFront();
ytplayer.TopMost = false;
ytplayer.Show();
this.Close();
}问题是我不太知道如何将一个函数发送到另一个函数。较低的代码可能会使我的问题更清楚一些。
private void settingsYTP(.....problem.....)
{
}发布于 2014-02-20 13:01:28
如果我明白的话,你需要这样的东西
private void btPlayMin_Click(object sender, EventArgs e)
{
settingsYTP(sender, e, link, "Normal", new Size(300, 24), new Size(400, 95), false, "", FormBorderStyle.FixedToolWindow, true);
int x = Screen.PrimaryScreen.WorkingArea.Width - 375;
ytplayer.Location = new Point(x, 0);
ytplayer.Show();
this.Close();
}
private void btPlayNorm_Click(object sender, EventArgs e)
{
settingsYTP(sender, e, link, "Minimal", new Size(300, 240), new Size(400,335), true, "YoutubePlayer", FormBorderStyle.FixedSingle, false);
ytplayer.Show();
this.Close();
}
private void settingsYTP(object sender, EventArgs e, string link, string minmax,Size size1, Size size2, bool b1, string text,FormBorderStyle bs, bool b2)
{
ytplayer.playLink(sender, e, link);
ytplayer.miniMax(minmax, size1, size2, b1, text, bs, b2);
ytplayer.TopMost = true;
ytplayer.BringToFront();
ytplayer.TopMost = false;
}发布于 2014-02-20 12:55:46
您可以按建议使用委托,但我更愿意查看Func<>和/或Action<>
MSDN示例:
public class GenericFunc
{
public static void Main()
{
// Instantiate delegate to reference UppercaseString method
Func<string, string> convertMethod = UppercaseString;
string name = "Dakota";
// Use delegate instance to call UppercaseString method
Console.WriteLine(convertMethod(name));
}
private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
}发布于 2014-02-20 12:51:35
你可以使用委托。
delegate ReturnType YourFunctionDelegate(argType yourArgument);http://msdn.microsoft.com/en-us/library/ms173171.aspx
例如,假设要传递的函数如下:
void MyFunction(int arg1, int arg2);您可以为它声明一个委托:
delegate void MyFunctionDelegate(int arg1, int arg2);您可以将委托看作表示具有特定签名的函数的类型。
现在,回到要传递MyFunction的函数。您可以通过指定接受委托YourFunctionDelegate的另一个参数来做到这一点。
private void settingsYTP(otherArguments, MyFunctionDelegate function)
{
// Some code ...
// Use the delegate function
function(1, 2);
// Some other cose...
}最后,只是
MyFunctionDelegate functionDelegate = MyFunction;
settingsYTP(otherArguments, functionDelegate);https://stackoverflow.com/questions/21908277
复制相似问题