如何获得自己班级的家长?

时间:2016-05-18 09:09:34

标签: c# class parent-child

我创建了一个或多或少的简单类,看起来像这样:

public class PreviewItem
{
    public string ItemName { get; set; }
    public Point PreviewStartingPoint { get; set; }
    public Point ReleaseStartingPoint { get; set; }
    public double PreviewWidth { get; set; }
    public double PreviewHeight { get; set; }
    public double ReleaseWidth { get; set; }
    public double ReleaseHeight { get; set; }
    public Border PreviewBorder { get; set; }
    public DispatcherTimer ShowTimer { get; set; }
    public int ShowIndex { get; set; }
    public List<MarketingItemNode> HandleList { get; set; }
}

介于两者之间我称之为:

previewItem.ShowTimer = new DispatcherTimer
{
    Interval = TimeSpan.FromMilliseconds(previewItem.HandleList[0].Duration)
};

previewItem.ShowTimer.Tick += ShowNextItem;
previewItem.ShowTimer.Start();

现在我给Timer一个正常的Tick事件。当事件触发时,我想找到发件人的“PreviewItem”。 像这样:

private void ShowNextItem(object sender, EventArgs eventArgs)
{
    DispatcherTimer thisTimer = (DispatcherTimer) sender;
    PreviewItem thisItem = (PreviewItem) thisTimer.Parent;
    ...
}

但是这个游戏没有父母。如何在Tick-Event中获得全班?

3 个答案:

答案 0 :(得分:1)

您可以按照建议使用Tag属性,也可以使用DispatcherTimer子类:

public class MyDispatcherTimer : DispatcherTimer
{
    public MyDispatcherTimer(PreviewItem parent)
    {
        ....
    }
}

答案 1 :(得分:1)

或者你可以内联方法:

previewItem.ShowTimer = new DispatcherTimer
{
    Interval = TimeSpan.FromMilliseconds(previewItem.HandleList[0].Duration)
};

previewItem.ShowTimer.Tick += (s, ee) =>
{
    // create your logic for the tick event here:
    previewItem.DoSomething();
};

previewItem.ShowTimer.Start();

但是,如果您需要在班级中声明计时器:public DispatcherTimer ShowTimer { get; set; },您最好让班级自己处理计时器。

您可以创建一个创建计时器的构造函数。

public class PreviewItem
{
   public string ItemName { get; set; }
   public Point PreviewStartingPoint { get; set; }
   public Point ReleaseStartingPoint { get; set; }
   public double PreviewWidth { get; set; }
   public double PreviewHeight { get; set; }
   public double ReleaseWidth { get; set; }
   public double ReleaseHeight { get; set; }
   public Border PreviewBorder { get; set; }
   public DispatcherTimer ShowTimer { get; private set; }   //  <-- private set
   public int ShowIndex { get; set; }
   public List<MarketingItemNode> HandleList { get; set; }

   public PreviewItem()
   {
       ShowTimer = new DispatcherTimer
       {
            Interval = TimeSpan.FromMilliseconds(previewItem.HandleList[0].Duration)
       };
       ShowTimer.Tick += ShowNextItem;
   }

   private void ShowNextItem(object sender, EventArgs eventArgs)
   {
        DispatcherTimer thisTimer = (DispatcherTimer) sender;
        PreviewItem thisItem = this;   // <-- for example, because you're in the object itself...
        ...
   }
}

使用它像:

var myItem = new PreviewItem();
myItem.ShowTimer.Start();

但这意味着每个PreviewItem都拥有自己的Timer (我认为DispatcherTimer不应该在此类中

我认为更好的方法是创建一个计时器来处理所有实例。 (迭代PreviewItem s)列表

答案 2 :(得分:0)

也许是这样的事情?

previewItem.ShowTimer = new DispatcherTimer
            {
                Interval = TimeSpan.FromMilliseconds(previewItem.HandleList[0].Duration),
                Tag = previewItem
            };
            previewItem.ShowTimer.Tick += ShowNextItem;
            previewItem.ShowTimer.Start();



private void ShowNextItem(object sender, EventArgs eventArgs)
    {
        DispatcherTimer thisTimer = (DispatcherTimer) sender;
        PreviewItem thisItem = (PreviewItem) thisTimer.Tag;
        ...
    }