C#委托方法丢失了类字段/事件丢失类字段

时间:2010-11-14 02:18:00

标签: c# events delegates

请帮忙。

我有几个Action1类的实例。他们每个人都应该在完成动画后导航到不同的页面。

无关紧要巫婆对象是“已调用”,它始终导航到同一页面。

只要我在“调用”方法中导航,导航就能正常工作。

看起来“PageAnimation_Completed”在同一个对象实例上一直被调用,为什么?

Stacks and Heaps是什么?如何解决这个问题?

我有以下课程:

public class Action1 : TriggerAction<DependencyObject>
{
    PhoneApplicationPage page;

    protected override void OnAttached() {
        ...
        page = (PhoneApplicationPage)elem;
        ...
    }

 [System.Windows.Interactivity.CustomPropertyValueEditorAttribute(System.Windows.Interactivity.CustomPropertyValueEditor.Storyboard)]
    public Storyboard PageAnimation { get; set; }

    public static readonly DependencyProperty Message = DependencyProperty.Register("IsSpinning", typeof(Uri), typeof(Action1), null);
    public Uri Page
    {
        get { return (Uri)GetValue(Message); }
        set { SetValue(Message, value); }
    }



    protected override void Invoke(object o)
    {
        PageAnimation.Completed += new EventHandler(PageAnimation_Completed);
        PageAnimation.Begin();
    }

    void PageAnimation_Completed(object sender, EventArgs e)
    {
        page.NavigationService.Navigate(new Uri("/" + this.Page.OriginalString, UriKind.RelativeOrAbsolute));
        PageAnimation.Stop();
    }
}

1 个答案:

答案 0 :(得分:1)

您需要取消订阅PageAnimation.Completed活动:

void PageAnimation_Completed(object sender, EventArgs e)
{
    PageAnimation.Completed -= PageAnimation_Completed;
    page.NavigationService.Navigate(new Uri("/" + this.Page.OriginalString, UriKind.RelativeOrAbsolute));
    PageAnimation.Stop();
}