-4
我有一個關於wpf和代表的問題。我試圖讓一個wpf應用程序像Windows窗體應用程序一樣工作。但我想我需要使用代表,我不知道這是如何工作。Wpf和代表
有人可以向我解釋如何做到這一點,我不問你是否想要我的代碼。我只是想要一個正確的方向。
Windows窗體代碼
Mainform.cs
private void CheckDatabase(bool fix)
{
String dbFileName = FileTextBox.Text;
if (!File.Exists(dbFileName))
{
MessageBox.Show(this, "File doesn't exist");
return;
}
Text = (fix ? "Fixing database" : "Checking database") + "'" + dbFileName + "'";
ContentPanel.Visible = false;
Logger logger = new Logger(Path.ChangeExtension(dbFileName, ".log"), this, LogTextBox);
var worker = new CheckDatabaseBackgroundWorker(dbFileName, fix, logger);
worker.RunWorkerCompleted += CheckDatabaseCompleted;
worker.Start();
}
Logger.cs
private readonly Form _form;
private readonly TextBox _textBox;
private readonly TextWriter _logFile;
private delegate void _delegate(String value);
public Logger(String fileName, Form form, TextBox textBox)
{
_form = form;
_textBox = textBox;
_logFile = new StreamWriter(fileName, true);
textBox.Clear();
}
private void DisplayText(String value)
{
_textBox.AppendText(value + Environment.NewLine);
}
public void Write(String msg, params Object[] args)
{
_form.Invoke(new _delegate(DisplayText), String.Format(msg, args));
//_textBox.AppendText(String.Format(msg, args) + Environment.NewLine);
_logFile.WriteLine(msg, args);
}
public String Write(Exception exc)
{
String msg = "Exception: " + exc;
_logFile.WriteLine(msg);
return msg;
}
public void Write(Exception exc, String msg, params Object[] args)
{
Write(msg, args);
_logFile.WriteLine("The exception was: " + exc);
}
public void Close()
{
_logFile.Close();
}
WPF代碼
個MainWindow.xaml.cs
public partial class MainWindow
{
private ICheckDatabase _checkDatabase = new Class1();
public MainWindow()
{
InitializeComponent();
}
private void OpenFileButtonClick(object sender, RoutedEventArgs e)
{
OpenFileDialog databasefiledialog = new OpenFileDialog
{
CheckPathExists = true,
CheckFileExists = true,
Filter = "Database Files(*.db, *.zip)|*.db;*.zip|All files(*.*)|*.*"
};
bool? result = databasefiledialog.ShowDialog();
if (result == true)
{
string fileName = databasefiledialog.FileName;
FileNameTextBox.Text = fileName;
}
}
private void CheckDatabaseButtonClick(object sender, RoutedEventArgs e)
{
}
private void FixDatabaseButtonClick(object sender, RoutedEventArgs e)
{
_checkDatabase.FixDatabase();
}
private void RemoveConfigButtonClick(object sender, RoutedEventArgs e)
{
String fileName = FileNameTextBox.Text;
Logger logger = new Logger(Path.ChangeExtension(FileNameTextBox.Text, ".log"), this, LogTextBox);
//logger.Write("");
//logger.Write("********");
//logger.Write("******** " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " : removing configurations from database '" + fileName + "'");
//logger.Write("********");
//_checkDatabase.RemoveConfigurations(fileName, logger);
//logger.Close();
}
private void PackDatabaseButtonClick(object sender, RoutedEventArgs e)
{
String dbFileName = FileNameTextBox.Text;
String zipFileName = Path.ChangeExtension(dbFileName, ".zip");
if (!File.Exists(dbFileName))
{
MessageBox.Show(this, "File '" + dbFileName + "' doesn't exist", "Error");
return;
}
}
}
WPFlogger.cs
public delegate void LoggerDelegate();
public class WpfLogger
{
private readonly Form _window;
private readonly TextBox _textBox;
private readonly TextWriter _logFile;
private delegate void _delegate(String value);
public WpfLogger(String fileName, LoggerDelegate loggerDelegate, TextBox textBox)
{
_window = window;
_textBox = textBox;
_logFile = new StreamWriter(fileName, true);
textBox.Clear();
}
private void DisplayText(String value)
{
_textBox.AppendText(value + Environment.NewLine);
}
public void Write(String msg, params Object[] args)
{
_window.Invoke(new _delegate(DisplayText), String.Format(msg, args));
//_textBox.AppendText(String.Format(msg, args) + Environment.NewLine);
_logFile.WriteLine(msg, args);
}
public String Write(Exception exc)
{
String msg = "Exception: " + exc;
_logFile.WriteLine(msg);
return msg;
}
public void Write(Exception exc, String msg, params Object[] args)
{
Write(msg, args);
_logFile.WriteLine("The exception was: " + exc);
}
public void Close()
{
_logFile.Close();
}
}
在此先感謝
STOP!你爲什麼要讓WPF像WinForms?....看看MVVM –
我希望它能像我的WinForms一樣。所以我可以瞭解更多關於WPF –
它不適用於學校,而僅僅是我的學習體驗 –