简体   繁体   English

代表在Windows C#WPF之间传递数据

[英]Delegates to Pass Data Between Windows C# WPF

I am attempting to use Delegates to pass data between two Windows . 我正在尝试使用Delegates在两个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 . 我想名单People ,当用户点击主显示按钮的Window ,这将然后让用户选择一个Person After doing this it will fill in a TextBox on the original form with the Person name. 完成此操作后,它将在原始表单上的“ TextBox ”中填写“ Person

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 . 我开始时要比这简单得多,这使我了解了Delegates并且在理解如何使用Delegates调用单独Windows上的方法时遇到了问题。 This is what I have attempted so far; 到目前为止,这是我尝试过的。

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

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

The second Window that contains the Delegate ; 第二个Window包含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'. 这确实显示了一条消息,指出“测试”,但是这样做后还会引发ExceptionDelegate 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 . MainWindow上的方法对我来说似乎并不正确,但是在尝试了不同的选择之后,这是我最接近工作的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. 像Mat链接一样,事件是一种特殊的委托,这可能正是您想要的。

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. 通常,您将创建一个从EventArgs继承的PersonEventArgs类。 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 这是一个带有示例的不错的链接: https : //msdn.microsoft.com/zh-cn/library/system.eventargs(v=vs.110).aspx

Good luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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