-4

I have a question about wpf and delegates. I'm trying to make a wpf application work the same way as a Windows Form application. But i think i need to use delegates for that and i do not know how that works.

Can someone explain to me how to do this, I am not asking if you want to make my code. I just want a push in the right direction.

Windows Forms code

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 code

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();
    }
}

Thanks in advance

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
The_Monster
  • 494
  • 2
  • 7
  • 28
  • 4
    STOP! Why would you want to make WPF like WinForms?....Look into MVVM – Justin Pihony Oct 29 '13 at 15:10
  • I want it to do the same as my WinForms. So i can learn more about WPF – The_Monster Oct 29 '13 at 15:11
  • it is not for school but just an learning experience for me – The_Monster Oct 29 '13 at 15:11
  • 2
    If you are deadset on this, though...could you try to limit your code down to what the problem is...or at least explain the problem better? – Justin Pihony Oct 29 '13 at 15:11
  • If it's a learning experience then your time would be much better spent learning MVVM, [this link](http://stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish) has some good information. – JMK Oct 29 '13 at 15:13
  • I do not want it to look the same way, i want to have the same functions as the WinForms. so i've made a secons logger class and called that WPFlogger.cs but i do not know how t make it work the same way as the windows forms application does – The_Monster Oct 29 '13 at 15:16
  • 1
    Your question, or the way it is formulated (is it formulated ?) is not specific enough. http://stackoverflow.com/questions/how-to-ask – franssu Oct 29 '13 at 15:23

1 Answers1

3
  1. Get rid of your logger. Don't reinvent the wheel. Use log4net, NLog or EntLib.
  2. Forget about 1-to-1 code samples. Learn about Model-View-ViewModel. Use MVVMLight or any other library which supports MVVM.

Cheers and good luck.

Darek
  • 4,687
  • 31
  • 47