如何填充列表框插件项名称,因为它在插件界面中设置?

时间:2016-07-11 13:25:50

标签: c# string dll plugins listbox

当加载插件按钮时,按钮会在方法中询问时填充IPlugin接口的名称  string Name { get; }

我正在尝试使用与按钮相同的方式填充列表框项目。

enter image description here

  

private void AssembleComponents

private void AssembleComponents(object sender)
        {
            ICollection<IPlugin> plugins = GenericPluginLoader<IPlugin>.LoadPlugins("Plugins");



            string selection = "";
            if (sender is ListBox)
            {
                if (((ListBox)sender).SelectedValue != null)
                    selection = ((ListBox)sender).SelectedValue.ToString();

            }

            string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
            DirectoryCatalog cat = new DirectoryCatalog(path);

            //ICollection<IPlugin> plugins = PluginLoader.LoadPlugins("Plugins");




            foreach (var item in plugins)
            {
                //add only if not already present
                if (!_Plugins.ContainsKey(item.Name))
                {
                    string dllName = GetDLLName(item.Name);


                    Button b = new Button() 
                    { 
                        Name = dllName.Replace(".", "").ToUpper(), 
                        Content = item.Name,
                        Visibility = System.Windows.Visibility.Hidden 
                    };

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

                    _Plugins.Add(item.Name, item);



                   // this.PluginGrid.Children.Clear();  
                   //by Vasey

                }
            }


            // make visible the selected plugin button
   foreach (var ctl in PluginGrid.Children)
   {
       if (ctl is Button)
       {
           Button button = (Button)ctl;

           if (button.Name.Equals(selection.Replace(".", "").ToUpper()))
           {

               button.Visibility = System.Windows.Visibility.Visible;
           }
           else
           {
                button.Visibility = System.Windows.Visibility.Hidden;
           }
       }
   }
        }
  

字符串GetDLLName

    string GetDLLName(string Name)
    {
        string ret = "";

        Name = Name.Replace(" ", ""); // strip spaces

        Assembly asm = AppDomain.CurrentDomain.GetAssemblies().
               SingleOrDefault(assembly => assembly.GetName().Name == Name);

        if (asm != null)
        {
            ret = Path.GetFileNameWithoutExtension(asm.Location);


        }

        return ret;
    }
  

private void lbFiles_SelectionChanged

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


    }

我该如何实现?

0 个答案:

没有答案