如何在C#中将参数传递给新的RoutedEventHandler方法?

时间:2016-10-30 13:26:49

标签: c#

我正在尝试创建一个应用程序启动器,最后我试图传递和额外的参数到RoutedEventHandler方法调用,但它给我一个错误,我试着在谷歌上查找但我不明白该死的东西。 (我是菜鸟)所以这里的代码请看看吧。谢谢

namespace Button_Launcher
{

    public partial class MainWindow : Window
    {
        private string line;

        public MainWindow()
        {
            InitializeComponent();

        }

        private void button_Click(object sender, RoutedEventArgs e)
        {

            OpenFileDialog fileName = new OpenFileDialog();
            fileName.Filter = "executables (.exe)|*.exe";
            fileName.ShowDialog();
            StreamWriter writer = new StreamWriter("E:/Config.txt",true);
            writer.WriteLine(fileName.FileName);
            writer.Close();
            textBox.Text = fileName.FileName;



        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string file = ("E:/Config.txt");

            StreamReader Reader = new StreamReader(file, Encoding.ASCII);
            List<string> appList = new List<string>();

            List<string> Items = new List<string>();
            int count = 1;
            while ((line = Reader.ReadLine()) != null)
            {
                makeButton(line,count);
                count++;
            }
            Reader.Close();

        }

        public void makeButton(string link,int count)
        {
            string[] mySplit = link.Split(new char[] { '\\' });
            string[] name = mySplit.Last().Split(new char[] { '.' });
            string fName = name.First();
            string buttonName = fName;
            Button newBtn = new Button();
            newBtn.Content = buttonName;
            newBtn.Height = 30;
            newBtn.Width = 70;
            newBtn.Margin =  new Thickness(-60, 90*count/1.5, 180,80);
            StackPanel sp = new StackPanel();
            sp.Children.Add(newBtn);
            mainGrid.Children.Add(sp);
            newBtn.Click += new RoutedEventHandler(launchApp(link));
            //launchApp(link);


        }
        private void launchApp(string link,object sender, RoutedEventArgs e)
        {

            Process.Start(link);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您无法更改RoutedEventHandler的方法签名。而是尝试将链接数据存储在按钮的Tag属性中。然后在该方法中,您可以将发送方对象强制转换为按钮并访问标记属性。

public void makeButton(string link,int count)
{
    string[] mySplit = link.Split(new char[] { '\\' });
    string[] name = mySplit.Last().Split(new char[] { '.' });
    string fName = name.First();
    string buttonName = fName;
    Button newBtn = new Button();
    newBtn.Content = buttonName;
    newBtn.Height = 30;
    newBtn.Width = 70;
    newBtn.Tag = link;
    newBtn.Margin =  new Thickness(-60, 90*count/1.5, 180,80);
    StackPanel sp = new StackPanel();
    sp.Children.Add(newBtn);
    mainGrid.Children.Add(sp);
    newBtn.Click += launchApp;
    //launchApp(link);


}
private void launchApp(object sender, RoutedEventArgs e)
{
    var btn = sender as Button;
    if( btn == null )
        return;

    Process.Start(btn.Tag.ToString( ));
}