链接对象丢失链接

时间:2014-02-13 17:06:44

标签: c# winforms automapper

嗯,我有点奇怪的问题,但我会尽力说明,因为我花了最近几天试图寻找解决方案但失败了。

在我的应用程序中,我有一个Windows窗体,其中包含许多像控件一样显示Employee's数据。自从我的应用程序开发开始以来,我使用了一种奇怪的方式来保持UI更新为Employee实体的任何更改,我将在下面的代码中演示。

Company company = new Company();

public Form1()
{
    InitializeComponent();

    company.Name = "Company Name";
    company.Employees = employees;

    this.Controls.Add(new EmployeeTile {Tag = company.employees[0]}); //I tag every tile with an employee from the array.
    this.Controls.Add(new EmployeeTile {Tag = company.employees[1]}); //I tag every tile with an employee from the array.
    this.Controls.Add(new EmployeeTile {Tag = company.employees[2]}); //I tag every tile with an employee from the array.
    this.Controls.Add(new EmployeeTile {Tag = company.employees[3]}); //I tag every tile with an employee from the array.
    //Then at the Employee Tile i have a timer which runs every couple of seconds and updates it self from the Tag object.
    //If any change made to the corresponding employee in the array it will be applied to the Tag of the Tile too.
}

这种方式运行良好,直到我使用自动映射器自动将更改应用于Employees

var comp = new Company()
{
    Name = "Company Name",
    Employees = /*NEW EMPLOYEE ARRAY WITH THE SAME COUNT AND EVERY THING BUT UPDATED VALUES*/
}
Mapper.Map<Company, Company>(/*FROM THE COMPANY OBJECT I NEWELY CREATED*/ comp, company /*TO THE EXISTING ONE*/);

一旦我这样做,ui就不再更新,好像某种方式Tag和它的相应数组成员之间的链接不再存在。尽管automapper的这种重载不应该创建新对象,只是将值复制到现有对象。

EmployeeTileControl

public EmployeeTile()
{
    InitializeComponent();

    //TIME USING REACTIVE EXTENSIONS.
    Observable.Interval(TimeSpan.FromSeconds(1), new ControlScheduler(this)).Subscribe(_ =>
    {
        Employee employee = Tag as Employee;
        if (null != employee)
        {
            this.lblEmployeeName.Text = string.Format("Name: {0}", employee.Name);
            this.lblEmployeeAge.Text = string.Format("Age: {0}", employee.Age);
        }
        //SO IT KEEPS UPDATING THE UI EVERY 1 SECOND WITH THE INFO FROM THE TAG.
        //AS IT SUPPOSED THAT THIS TAG IS REFLECTING THE `ORIGINAL` EMPLOYEE INSTANCE FROM THE ARRAY.
        //SO IT MUST BE UPDATED WHEN EVERY THE ORIGINAL INSTANCE IS UPDATED.
    });
}
NB:我是一个非常新的程序员,所以我可能会用错误的术语提到某些东西,我希望你纠正我。标题可能也是非常错误的,但我不知道我应该怎么称呼它。

0 个答案:

没有答案
相关问题