【问题标题】:How do I add the button/plugin IF AND ONLY IF it's not already there?如果且仅当按钮/插件不存在时,如何添加按钮/插件?
【发布时间】:2016-07-05 17:26:30
【问题描述】:

I changed the code to use a stack panel and got this, from repeated clicks

如何实现它,以便它只在其空白时添加按钮/插件,而在堆栈面板已填充时不添加它们??

下面是代码:

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


        }

       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;
                stackPanel.Children.Add(b);
                b = null;
                //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);
            if (!File.Exists(System.IO.Path.Combine(path, fi.Name)))
            {
                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)
        { 

            lbFiles.Items.Clear();


        }

    }
}

【问题讨论】:

    标签: c# wpf plugins stackpanel


    【解决方案1】:

    您可以使用 Count 方法来检查您的堆栈面板是否包含项目,请尝试以下操作:

    if (stackpanel.Children.Count <= 0)
    {
        _Plugins.Add(item.Name, item);
        stackPanel.Children.Add(b);
    }
    

    【讨论】:

    • 它不再重复,但它也没有加载第二个插件,也没有为两个插件执行函数 if (PluginGrid.Children.Count
    猜你喜欢
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2015-11-17
    • 2013-04-07
    • 2021-08-23
    • 2015-06-21
    相关资源
    最近更新 更多