简体   繁体   中英

Delegates to Pass Data Between Windows C# WPF

I am attempting to use Delegates to pass data between two Windows . I want a list of People to appear when the user clicks a button on the main Window , this will then allow the user to select a Person . After doing this it will fill in a TextBox on the original form with the Person name.

I started off much simpler than this however to get my head around Delegates and I'm having issues understanding how to call methods on separate Windows with the Delegates . This is what I have attempted so far;

The method on the main Window that waits for a string (ShowMessage);

public static DelegateTestWindow.TestDelegate ShowDelegateMessage(string message)
{
    MessageBox.Show(message);
    return null;
}

The second Window that contains the Delegate ;

public partial class DelegateTestWindow : Window
{
    public delegate string TestDelegate();
    public DelegateTestWindow()
    {
        InitializeComponent();
    }

    private void TestDelegateClick(object sender, RoutedEventArgs e)
    {
        var tDelegate = new TestDelegate(CompanyManagement.ShowDelegateMessage("Test"));
        this.Close();
    }
}

This does show a message saying "Test" however it also throws an Exception after doing so: Delegate to an instance method cannot have null 'this'.

The method on the MainWindow especially does not look correct to me, however after trying different options this is the closest I've got to a working Delegate . Could someone explain where I have (clearly) gone wrong here and how I can improve the system I've got now?

Like Mat linked, events are a special kind of delegate, which is probably what you are looking for.

I've written a small example, hopefully this is enough for you to resolve your issue.

public partial class Form1 : Form
{
    public event EventHandler PersonSelected;
    public String PersonName { get; set; }

    public Form1()
    {
        InitializeComponent();
        PersonName = "Person's name";
    }

    protected override void OnClick(EventArgs e)
    {
        Form2 form2 = new Form2(this);
        form2.Show();
        PersonSelected(this, EventArgs.Empty);
    }
}

public partial class Form2 : Form
{
    public Form2(Form1 form)
    {
        InitializeComponent();
        form.PersonSelected += (sender, args) =>
        {
            Form1 form1 = sender as Form1;
            if(form1 != null)
                textBox1.Text = form1.PersonName;
        };
    }
}

Now, this can be done differently, and better than this, however I think this should suffice. When you get this to work, I recommend you look into another way of doing this. Typically you will create a PersonEventArgs class which inherits from EventArgs. This class then holds a reference to the person. Here is a decent link with an example: https://msdn.microsoft.com/en-us/library/system.eventargs(v=vs.110).aspx

Good luck :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM