How do I populate the listbox plugin item Name as it is set in the Plugin interface?

扶醉桌前 提交于 2019-12-12 03:52:07

问题


When the plugin buttons get loaded, the button gets populated with the name of the IPlugin Interface as it asks in the method string Name { get; }

I’m trying to get the listbox item to populate with the name the same way its doing so for the button.

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

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


    }

How can I implement this?

来源:https://stackoverflow.com/questions/38308186/how-do-i-populate-the-listbox-plugin-item-name-as-it-is-set-in-the-plugin-interf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!