创造功能的功能

时间:2014-02-20 12:48:06

标签: c# winforms function

我正在尝试在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.....)
{
}

3 个答案:

答案 0 :(得分:2)

您可以按建议使用委托,但我更愿意查看Func<>和/或行动<>

来自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();
   }
}

答案 1 :(得分:0)

您可以使用代表。

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);

答案 2 :(得分:0)

如果我理解你需要这样的东西

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;
}