首页 > 解决方案 > 如何将功能从界面移动到单独的程序集[错误]

问题描述

我目前正在开发一种文件复制工具,该工具允许我为要从中复制的文件夹选择源和目标。用户单击复制后会显示一个进度条。

唯一的问题是我的所有函数都驻留在一个文件中,即 form1.cs(如下)

namespace CopyFacility
{
    public partial class Form1 : Form
    {

        BackgroundWorker background = new BackgroundWorker();
        FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
        OpenFileDialog openFile = new OpenFileDialog();


        public Form1()
        {
            InitializeComponent();
            background.WorkerSupportsCancellation = true;
            background.WorkerReportsProgress = true;
            background.DoWork += Background_DoWork;
            background.RunWorkerCompleted += Background_RunWorkerCompleted;
            background.ProgressChanged += Background_ProgressChanged;

        }

        string inputFile = null;
        string outputFile = null;


        private void CopyFile(string source, string destination, DoWorkEventArgs e)
        {
            FileStream fsOut = new FileStream(destination, FileMode.Create);
            FileStream fsIn = new FileStream(source, FileMode.Open);
            byte[] buffer = new byte[1048756];
            int readBytes;
            while((readBytes = fsIn.Read(buffer,0,buffer.Length)) > 0)
            {
                if(background.CancellationPending)
                {
                    e.Cancel = true;
                    background.ReportProgress(0);
                    fsIn.Close();
                    fsOut.Close();
                    return;
                }
                else
                {
                    fsOut.Write(buffer, 0, readBytes);
                    background.ReportProgress((int) (fsIn.Position * 100 / fsIn.Length));
                }


                fsOut.Write(buffer, 0, readBytes);
                background.ReportProgress((int)(fsIn.Position * 100 / fsIn.Length));
            }

            fsIn.Close();
            fsOut.Close();

        }

        private void Background_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            fileProgressBar.Value = e.ProgressPercentage;
        }

        private void Background_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if(e.Cancelled)
            {
                fileProgressBar.Visible = true;
                lblMessage.Visible = true;
                lblMessage.Text = "The process has been cancelled";
            }
            else
            {
                fileProgressBar.Visible = true;
                lblMessage.Visible = true;
                lblMessage.Text = "The process has been completed";
            }
        }

        private void Background_DoWork(object sender, DoWorkEventArgs e)
        {
            CopyFile(inputFile, outputFile + @"\" + Path.GetFileName(inputFile),e);

        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            background.CancelAsync();
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            if(background.IsBusy)
            {
                lblProgress.Visible = true;
            }
            else
            {
                fileProgressBar.Visible = true;
                background.RunWorkerAsync();
            }
        }

        private void btnSource_Click(object sender, EventArgs e)
        {
            if(openFile.ShowDialog() == DialogResult.OK )
            {
                inputFile = openFile.FileName;
                btnSource.Text = inputFile;
            }
        }

        private void btnDestination_Click(object sender, EventArgs e)
        {
            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                outputFile = folderBrowser.SelectedPath;
                btnDestination.Text = outputFile + @"\" + Path.GetFileName(inputFile);
            }
        }


    }
}

我想知道如何将函数“CopyFile”放入它自己的类中,只要单击按钮就可以调用它?

当我尝试创建新的类方法并将与复制函数相关的函数插入新类“CopyFunction.cs”时,代码“InitializingComponent();”出现以下错误 如下

public CopyPresenter(BackgroundWorker background, FolderBrowserDialog folderBrwoser, OpenFileDialog openFile)
        {

            InitializeComponent();

            background.WorkerSupportsCancellation = true;
            background.WorkerReportsProgress = true;
            background.DoWork += Background_DoWork;
            background.RunWorkerCompleted += Background_RunWorkerCompleted;
            background.ProgressChanged += Background_ProgressChanged;
        }

该错误表示当前上下文中不存在“InitializeComponent”。

标签: c#

解决方案


推荐阅读