c# - 如何将功能从界面移动到单独的程序集[错误]
问题描述
我目前正在开发一种文件复制工具,该工具允许我为要从中复制的文件夹选择源和目标。用户单击复制后会显示一个进度条。
唯一的问题是我的所有函数都驻留在一个文件中,即 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”。
解决方案
推荐阅读
- angular - 试图了解 Angular 7 应用程序中的文件结构
- java - connerDb 方法代表什么?
- r - 谁能帮我在 rbacon 中添加一个衰退?它看起来很简单,但它总是抛出错误
- wordpress - 在 5.2.1 版(2019 年 5 月)中在 wordpress 中创建新帖子时如何证明段落块的合理性
- angular - Angular 7 HttpClient 获取请求未定义的响应
- javascript - 在 React 中返回数组时如何添加换行符?
- java - Java:如何从 Google Dialogflow V2 响应中提取参数
- reactjs - 标记后每 N 秒向服务器请求一次
- sql - oracle过滤重复的列值
- php - PHP - 动态添加复选框时如何自动增加值和名称