所以我正在处理WPF
中的问题。
当您单击带有RichTextBox
作为输出数据框的按钮时,我有一个页面正在加载。
问题是,它检查FTP
上的文件是否可访问,但是它首先进行检查,因此需要一段时间,然后显示带有输出的页面。
我首先要显示带有输出框的页面,然后它将开始显示数据(就像您在安装程序中看到的那样)。
这是页面本身中的代码:
public partial class ArtaActionsPage : Page
{
public ArtaActionsPage()
{
InitializeComponent();
Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
AddBlackLine("");
AddBlackLine("Checking if everything is ready...");
AddBlackLine("");
bool AdobeReady = false;
bool SevenZipReady = false;
bool JavaReady = false;
bool EsetReady = false;
if (Global_Action_Variables.Arta_Variables.ArtaAdobeSwitch == true)
{
AddBlackLine("Checking for Adobe installator...");
bool CheckForAdobe = General_Functions.CheckIfFileExistsOnFTP("Ultra_Script/Basic_SW/Adobe_Reader.exe");
if(CheckForAdobe == true)
{
AddGreenLine("Installer for Adobe accesible.");
AdobeReady = true;
}
else
{
AddRedLine("Installer not accesible on FTP!!! Canceling job...");
AddRedLine("Check FTP repozitary if file in Ultra_Script/Basic_SW/Adobe_Reader.exe exists and ftp account have enough rights for this file.");
}
}
if (Global_Action_Variables.Arta_Variables.ArtaSevenZip == true)
{
AddBlackLine("");
AddBlackLine("");
AddBlackLine("Checking for SevenZip installer...");
bool CheckForSevenZip = General_Functions.CheckIfFileExistsOnFTP("Ultra_Script/Basic_SW/7zip.exe");
if(CheckForSevenZip == true)
{
AddGreenLine("Installer for 7zip accesible.");
SevenZipReady = true;
}
else
{
AddRedLine("Installer not accesible on FTP!!! Canceling job...");
AddRedLine("Check FTP repozitary if file in Ultra_Script/Basic_SW/7zip.exe exists and ftp account have enough rights for this file.");
}
}
if(Global_Action_Variables.Arta_Variables.ArtaJava == true)
{
AddBlackLine("");
AddBlackLine("");
AddBlackLine("Checking for Java installer...");
bool CheckForJava = General_Functions.CheckIfFileExistsOnFTP("Ultra_Script/Basic_SW/JaVa.exe");
if(CheckForJava == true)
{
AddGreenLine("Installer for Java accesible");
JavaReady = true;
}
else
{
AddRedLine("Installer not accesible on FTP!!! Canceling job...");
AddRedLine("Check FTP repozitary if file in Ultra_Script/Basic_SW/JaVa.exe exists and ftp account have enough rights for this file.");
}
}
if(Global_Action_Variables.Arta_Variables.ArtaEset == true)
{
AddBlackLine("");
AddBlackLine("");
AddBlackLine("Checking for Arta ESET installer...");
bool CheckForArtaEset = General_Functions.CheckIfFileExistsOnFTP("Ultra_Script/Basic_SW/Arta/ERA_ESET.exe");
if(CheckForArtaEset == true)
{
AddGreenLine("Installer for ESET accesible");
EsetReady = true;
}
else
{
AddRedLine("Installer not accesible on FTP!!! Canceling job...");
AddRedLine("Check FTP repozitary if file in UUltra_Script/Basic_SW/Arta/ERA_ESET.exe exists and ftp account have enough rights for this file.");
}
}
if(AdobeReady == true)
{
AddBlackLine("");
AddBlackLine("");
AddBlackLine("Test");
General_Functions.DownloadFileFromFTP("Ultra_Script/Basic_SW/Adobe_Reader.exe", "Adobe_Reader.exe");
}
else
{
AddBlackLine("");
AddRedLine("Failed.");
}
}
private void AddRedLine(string text)
{
TextRange tr = new TextRange(outputBox.Document.ContentEnd, outputBox.Document.ContentEnd);
tr.Text = text;
tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
outputBox.AppendText("\r");
outputBox.ScrollToEnd();
}
private void AddGreenLine(string text)
{
TextRange tr = new TextRange(outputBox.Document.ContentEnd, outputBox.Document.ContentEnd);
tr.Text = text;
tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Green);
outputBox.ScrollToEnd();
}
private void AddBlackLine(string text)
{
TextRange tr = new TextRange(outputBox.Document.ContentEnd, outputBox.Document.ContentEnd);
tr.Text = text;
tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
outputBox.AppendText("\r");
outputBox.ScrollToEnd();
}
}
这是显示新页面的按钮代码:
private void ArtaDoITButton_Click(object sender, RoutedEventArgs e)
{
if (File.Exists("settings.xml"))
{
XmlSerializer xs = new XmlSerializer(typeof(Information));
FileStream read = new FileStream("settings.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
Information info = (Information)xs.Deserialize(read);
try
{
string a = info.FtpPassword;
string FTPPassword = EncryDecryptor.Decrypt(a);
bool TestFTP = General_Functions.isValidConnection(info.HDSynologyIP, info.FtpUsername, FTPPassword);
if(TestFTP == true)
{
ArtaActionsFrame.Content = new ArtaActionsPage();
}
else
{
MessageBox.Show("Failed login to FTP repozitary. Check your credentials in settings.");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
read.Close();
}
read.Close();
}
else
{
MessageBox.Show("Chybí settings file!!");
}
}
页面的初始化:
public ArtaActionsPage()
{
InitializeComponent();
Loaded += OnLoaded;
}
有人知道如何优化它,以便它不会在加载页面之前进行检查吗?
感谢所有答案。
答案 0 :(得分:0)
您必须使用Page
/ async
异步初始化await
。所有初始化过程都应移至ArtaActionsPage
的公共InitalizeAsync()
方法中:
加载页面事件处理程序:
private async void ArtaDoITButton_Click(object sender, RoutedEventArgs e)
{
if (!File.Exists("settings.xml"))
{
MessageBox.Show("Chybí settings file!!");
return;
}
var nextPage = new ArtaActionsPage();
// Show the page immediately
ArtaActionsFrame.Content = nextPage;
if (!await nextPage.TryInitializeAsync())
{
MessageBox.Show("Failed login to FTP repozitary. Check your credentials in settings.");
// Show previous page again
}
}
Page.xaml.cs:
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{}
public async Task<bool> TryInitializeAsync()
{
bool ftpIsValid = await ValidateFtpAsync();
if (!ftpIsValid)
{
return false;
}
await CheckIfFileExistsOnFTPAsync();
return true;
}
private async Task<bool> ValidateFtpAsync()
{
return await Task.Run(
() =>
{
var xs = new XmlSerializer(typeof(Information));
using (var read = new FileStream("settings.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
{
Information info = (Information) xs.Deserialize(read);
try
{
string a = info.FtpPassword;
string fTPPassword = EncryDecryptor.Decrypt(a);
return General_Functions.isValidConnection(info.HDSynologyIP, info.FtpUsername, FTPPassword);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
return false;
});
}
public async Task CheckIfFileExistsOnFTPAsync()
{
bool ftpIsValid = await ValidateFtpAsync();
if (!ftpIsValid)
{
return false;
}
await Task.Run(
() =>
{
AddBlackLine("");
AddBlackLine("Checking if everything is ready...");
AddBlackLine("");
bool AdobeReady = false;
bool SevenZipReady = false;
bool JavaReady = false;
bool EsetReady = false;
if (Global_Action_Variables.Arta_Variables.ArtaAdobeSwitch == true)
{
AddBlackLine("Checking for Adobe installator...");
bool CheckForAdobe = General_Functions.CheckIfFileExistsOnFTP("Ultra_Script/Basic_SW/Adobe_Reader.exe");
if (CheckForAdobe == true)
{
AddGreenLine("Installer for Adobe accesible.");
AdobeReady = true;
}
else
{
AddRedLine("Installer not accesible on FTP!!! Canceling job...");
AddRedLine(
"Check FTP repozitary if file in Ultra_Script/Basic_SW/Adobe_Reader.exe exists and ftp account have enough rights for this file.");
}
}
if (Global_Action_Variables.Arta_Variables.ArtaSevenZip == true)
{
AddBlackLine("");
AddBlackLine("");
AddBlackLine("Checking for SevenZip installer...");
bool CheckForSevenZip = General_Functions.CheckIfFileExistsOnFTP("Ultra_Script/Basic_SW/7zip.exe");
if (CheckForSevenZip == true)
{
AddGreenLine("Installer for 7zip accesible.");
SevenZipReady = true;
}
else
{
AddRedLine("Installer not accesible on FTP!!! Canceling job...");
AddRedLine(
"Check FTP repozitary if file in Ultra_Script/Basic_SW/7zip.exe exists and ftp account have enough rights for this file.");
}
}
if (Global_Action_Variables.Arta_Variables.ArtaJava == true)
{
AddBlackLine("");
AddBlackLine("");
AddBlackLine("Checking for Java installer...");
bool CheckForJava = General_Functions.CheckIfFileExistsOnFTP("Ultra_Script/Basic_SW/JaVa.exe");
if (CheckForJava == true)
{
AddGreenLine("Installer for Java accesible");
JavaReady = true;
}
else
{
AddRedLine("Installer not accesible on FTP!!! Canceling job...");
AddRedLine(
"Check FTP repozitary if file in Ultra_Script/Basic_SW/JaVa.exe exists and ftp account have enough rights for this file.");
}
}
if (Global_Action_Variables.Arta_Variables.ArtaEset == true)
{
AddBlackLine("");
AddBlackLine("");
AddBlackLine("Checking for Arta ESET installer...");
bool CheckForArtaEset = General_Functions.CheckIfFileExistsOnFTP("Ultra_Script/Basic_SW/Arta/ERA_ESET.exe");
if (CheckForArtaEset == true)
{
AddGreenLine("Installer for ESET accesible");
EsetReady = true;
}
else
{
AddRedLine("Installer not accesible on FTP!!! Canceling job...");
AddRedLine(
"Check FTP repozitary if file in UUltra_Script/Basic_SW/Arta/ERA_ESET.exe exists and ftp account have enough rights for this file.");
}
}
if (AdobeReady == true)
{
AddBlackLine("");
AddBlackLine("");
AddBlackLine("Test");
General_Functions.DownloadFileFromFTP("Ultra_Script/Basic_SW/Adobe_Reader.exe", "Adobe_Reader.exe");
}
else
{
AddBlackLine("");
AddRedLine("Failed.");
}
});
}
还考虑将General_Functions.CheckIfFileExistsOnFTP()
转换为async
方法。
现在将立即显示ArtaActionsPage
。无论初始化是否失败,InitializeAsync
都会返回bool
。如果失败了,您可以处理这种情况,例如通过显示您的消息,然后导航回到上一页。