[英]Images not displaying on Winform.show c#
我有一个程序,当按下按钮时显示“正在加载”Winform,并在需要运行的脚本完成后消失。
当按下按钮时,新表单“出现”但它不显示任何表单信息,例如徽标和标签 - 只有一个空白/灰色框。 我尝试更改背景颜色并更改图像,但它仍然显示为空白表单。
我发现最令人困惑的是,显示的这个空白表单仅在特定 CS 时才显示为空白。 在按钮按下时调用文件; PDFMerge.CombineMultiblePDFs 。 如果我尝试在程序的不同部分显示加载表单,例如当按下不同的按钮时,表单会按计划正确加载所有内容。
这是显示的空白表单:
这是显示在不同按钮或不同表单上的正确表单
这是我正在调用的代码,它显示“空白”Winform。
loadingPDF.Show(); // Show the loading form
string fileDate = DateTime.Now.ToString("dd-MM-yy");
string fileTime = DateTime.Now.ToString("HH.mm.ss");
string outcomeFolder = outputFolder;
string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".pdf";
string outputFileName = Path.Combine(outcomeFolder, outputFile);
// combines the file name, output path selected and the yes / no for pagebreaks.
PDFMerge.CombineMultiplePDFs(sourceFiles, outputFileName);
loadingPDF.Hide(); // Hide the loading form
如果我用不同的 CS 文件替换PDFMerge.Combine ,加载表单会正确显示,这让我相信问题出在 PDFMerge 以及何时调用它。 以下是 PDFMerge 中使用的代码;
public class PDFMerge
{
public static void CombineMultiplePDFs(String[] fileNames, string outFile)
{
try
{
int pageOffset = 0;
int f = 0;
Document document = null;
PdfCopy writer = null;
while (f < fileNames.Length)
{
// Create a reader for a certain document
PdfReader reader = new PdfReader(fileNames[f]);
reader.ConsolidateNamedDestinations();
// Retrieve the total number of pages
int n = reader.NumberOfPages;
pageOffset += n;
if (f == 0)
{
// Creation of a document-object
document = new Document(reader.GetPageSizeWithRotation(1));
// Create a writer that listens to the document
writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));
// Open the document
document.Open();
}
// Add content
for (int i = 0; i < n;)
{
++i;
if (writer != null)
{
PdfImportedPage page = writer.GetImportedPage(reader, i);
writer.AddPage(page);
}
}
PRAcroForm form = reader.AcroForm;
if (form != null && writer != null)
{
//writer.CopyAcroForm(reader);
writer.Close();
}
f++;
}
// Close the document
if (document != null)
{
document.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
我不知道是什么原因导致与表单显示发生冲突,也许表单没有按时加载,但我不知道它如何与某些功能而不是其他功能一起使用。 任何有关该问题的建议将不胜感激。 谢谢
更新 1:请求附加代码,这是用于加载 PDF 表单的代码。 我使用 Winforms 在表单上创建内容:
public partial class LoadingPDF : Form
{
public LoadingPDF()
{
InitializeComponent();
}
private void LoadingPDF_Load(object sender, EventArgs e)
{
//
}
}
在文件选择表单中创建 loadingPDF 表单的实例
// Declaring the 'loading' form when files are being combined.
LoadingPDF loadingPDF = new LoadingPDF();
基于注释,PDFMerge.CombineMultiplePDFs() 正在 cpu 锁定您的程序,导致线程在完成之前停止加载表单。 您可以像这样调整代码:
public void ShowLoading()
{
loadingPDF.Shown += loadingPDF_Shown;
loadingPDF.Show(); // Show the loading form
}
public void loadingPDF_Shown(object sender, eventargs e)
{
string fileDate = DateTime.Now.ToString("dd-MM-yy");
string fileTime = DateTime.Now.ToString("HH.mm.ss");
string outcomeFolder = outputFolder;
string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".pdf";
// combines the file name, output path selected and the yes / no for pagebreaks.
PDFMerge.CombineMultiplePDFs(sourceFiles, outputFileName);
loadingPDF.Hide(); // Hide the loading form
}
显示的是加载表单时要触发的最后一个事件。 这应该在您开始 CPU 密集型进程之前加载您的图像。
另一种方法是将 CPU 密集型进程放在另一个线程上,以保持 UI 线程清晰。 你可以这样做:
public void ShowLoading()
{
loadingPDF.Show(); // Show the loading form
System.ComponentModel.BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.RunWorkerAsync(); //Added missed line
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//anything you want to do AFTER the cpu-intensive process is done
loadingPDF.Hide(); // Hide the loading form
}
public void worker_DoWork(object sender, DoWorkEventArgs e)
{
string fileDate = DateTime.Now.ToString("dd-MM-yy");
string fileTime = DateTime.Now.ToString("HH.mm.ss");
string outcomeFolder = outputFolder;
string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".pdf";
string outputFileName = Path.Combine(outcomeFolder, outputFile);
// combines the file name, output path selected and the yes / no for pagebreaks.
PDFMerge.CombineMultiplePDFs(sourceFiles, outputFileName);
}
使用后台工作人员执行此操作将使您的 UI 可用/可点击,而不会使其冻结。 除此之外,这允许动画加载表单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.