wp7,silverlight,为什么事件发生两次?

时间:2010-11-29 16:41:23

标签: silverlight events windows-phone-7

在这种情况下,当用户点击一些UIElement时,我希望这个元素被虚线边框包围。如果包含其他一些对象,则此代码首先将其删除。

一切都很好,但“private void activateElem”会发射两次,我不明白为什么。也许有人可以帮忙,我真的没有更多的想法。

有人知道调试这样的问题吗?也许有一些方法可以打印出所有事件队列。有银光间谍,但我认为它不适用于Windows Phone 7。

我的代码:

 public class ManipulationEngine
{
    private Canvas sheet;
    private static FrameworkElement current_active_element;

    public ManipulationEngine(Canvas sheet)
    {
        this.sheet = sheet;

        foreach (FrameworkElement elem in sheet.Children)
        {
            elem.MouseLeftButtonUp += new MouseButtonEventHandler(activateElem);
        }

    }

    private void activateElem(object sender, MouseButtonEventArgs e)
    {
        FrameworkElement elem = sender as FrameworkElement;

        if (current_active_element != null)
        {
            desactivateElem();
        }

        Grid grid = new Grid();
        Rectangle recentagle = new Rectangle();

        grid.SetValue(Canvas.TopProperty, (double)elem.GetValue(Canvas.TopProperty) - 10);
        grid.SetValue(Canvas.LeftProperty, (double)elem.GetValue(Canvas.LeftProperty) - 10);

        DoubleCollection stroke = new DoubleCollection();
        stroke.Add(4);
        stroke.Add(2);
        recentagle.StrokeDashArray = stroke;

        grid.Children.Add(recentagle);
        sheet.Children.Remove(elem);
        elem.Margin = new Thickness(10);
        grid.Children.Add(elem);
        sheet.Children.Add(grid);

        current_active_element = elem;
    }



    private void desactivateElem()
    {
        if (current_active_element != null)
        {
            Grid grid = VisualTreeHelper.GetParent(current_active_element) as Grid;

            grid.Children.Remove(current_active_element);
            sheet.Children.Remove(grid);

            current_active_element.SetValue(Canvas.TopProperty, (double)grid.GetValue(Canvas.TopProperty) + 10);
            current_active_element.SetValue(Canvas.LeftProperty, (double)grid.GetValue(Canvas.LeftProperty) + 10);
            current_active_element.Margin = new Thickness(0);

            sheet.Children.Add(current_active_element);

            current_active_element = null;
        }
    }

1 个答案:

答案 0 :(得分:0)

我真的建议查看零件和状态模型。您可以使用按钮或单选按钮来执行此操作。

通常,如果您正在对可视化树进行编码更改,那么您的表现并不正确。

Karen Corby在MIX08处理得非常清楚,看看吧!

http://archive.visitmix.com/blogs/2008Sessions/T20/