wpf とデリゲートについて質問があります。wpf アプリケーションを Windows フォーム アプリケーションと同じように動作させようとしています。しかし、そのためにデリゲートを使用する必要があると思いますが、それがどのように機能するかわかりません。
誰かがこれを行う方法を説明してもらえますか?私のコードを作成するかどうかを尋ねているわけではありません。正しい方向にプッシュしたいだけです。
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();
}
}
前もって感謝します