在Listbox项目中加载插件不单独执行selectionChanged上的函数

时间:2016-07-05 14:59:12

标签: c# wpf listbox mef

AssembleComponent Method

当我将第一个插件加载到列表框中时,它会执行第一个插件函数。

Code and WPF on runtime

然而,在我将第二个插件加载到列表框后,第一个插件函数更改为列出的两个项目的第二个插件函数,我不能再执行第一个插件函数。

当我点击列表框中的指定项目时,我需要单独执行每个功能。

你有没有机会帮我解决我在列表框选择方面遇到的问题?

MainWindow代码

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using PluginContracts;
using System;
using System.IO;
using Microsoft.Win32;

namespace SimplePlugin
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Dictionary<string, IPlugin> _Plugins;


        public MainWindow()
        {
            InitializeComponent();


        }
        Button b = new Button();
       private void AssembleComponents(object sender)
       {
             _Plugins = new Dictionary<string, IPlugin>();
            //ICollection<IPlugin> plugins = PluginLoader.LoadPlugins("Plugins");
            ICollection<IPlugin> plugins = GenericPluginLoader<IPlugin>.LoadPlugins("Plugins");
            foreach(var item in plugins)
            {
                Button b = new Button();
                //if exists an object, 
                if (b!= null)
                {


                    //delete that object, instantiate new object
                _Plugins.Add(item.Name, item);

                b.Content = item.Name;
                b.Click += b_Click;
                PluginGrid.Children.Add(b);  

                }





            }



       }


        private void b_Click(object sender, RoutedEventArgs e)
        {



            Button b = sender as Button;
            if(b != null)
            {
                string key = b.Content.ToString();
                if(_Plugins.ContainsKey(key))
                {
                    IPlugin plugin = _Plugins[key];
                    plugin.Do();



                }

            }



        }

        private void addPlugin_Click(object sender, RoutedEventArgs e)
        {

            var fileDialog = new OpenFileDialog();

            fileDialog.Multiselect = true;
            fileDialog.Filter = "All files (*.*)|*.*|DLL files (*.dll)|*.dll|CS Files (*.cs)|*.cs"; 


            if (fileDialog.ShowDialog() == true)
            {
                string filename = fileDialog.FileName;
                var ext = System.IO.Path.GetExtension(filename);

                 // ListBox lbFiles = new ListBox();

                //this.Controls.Add(lbFiles);
                //lbFiles.Size = new System.Drawing.Size(200, 100);
                //lbFiles.Location = new System.Drawing.Point(10, 10);

                lbFiles.Items.Add(System.IO.Path.GetFileName(filename));
              //  

                CopyToDir(filename);

            }
        }

        //private void lbFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    AssembleComponents();
        //}

        private void CopyToDir(string filename)
        {
            //  txtBox.Text = "Hello World";  
            string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
            Console.WriteLine(path);
            //Check the directory exists
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            try 
            {  
            FileInfo fi = new FileInfo(filename);

            File.Copy(fi.FullName, System.IO.Path.Combine(path, fi.Name)); 

            }
            catch (Exception ex)
            {
                throw ex; 
            }

        }


        //Approach 1
        //if exists an object, delete that object, instantiate new object
        private void lbFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           AssembleComponents(sender);

        }  
        //Approach 2 
        private void ClearBtn_Click(object sender, RoutedEventArgs e)
        { 
            b = null;
            lbFiles.Items.Clear();


        }

    }
}

0 个答案:

没有答案