Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未调用附加覆盖的Silverlight行为_C#_Silverlight_Events_Memory Leaks_Behavior - Fatal编程技术网

C# 未调用附加覆盖的Silverlight行为

C# 未调用附加覆盖的Silverlight行为,c#,silverlight,events,memory-leaks,behavior,C#,Silverlight,Events,Memory Leaks,Behavior,我已经创建了一个自定义行为(没有混合),用于拖动UIElements并获得光标下拖动内容的半透明缩略图(混合版本移动目标对象,这不是我需要的) 总之,代码实际上非常简单并且工作得很好,我遇到的问题是没有调用onDetaching(),这意味着我对UIElement的事件没有被取消挂钩 这让我有点担心,因为我猜行为没有被分离的唯一原因是因为它仍然被某些东西引用。它不应该是UIElement,虽然它是自己的,因为我们在某个阶段遇到了泄漏问题,但我们现在已经解决了这些问题,这通过WinDbg得到了澄清

我已经创建了一个自定义行为(没有混合),用于拖动UIElements并获得光标下拖动内容的半透明缩略图(混合版本移动目标对象,这不是我需要的)

总之,代码实际上非常简单并且工作得很好,我遇到的问题是没有调用onDetaching(),这意味着我对UIElement的事件没有被取消挂钩

这让我有点担心,因为我猜行为没有被分离的唯一原因是因为它仍然被某些东西引用。它不应该是UIElement,虽然它是自己的,因为我们在某个阶段遇到了泄漏问题,但我们现在已经解决了这些问题,这通过WinDbg得到了澄清

唯一有趣的是,它的行为被附加到items控件中的一个图像上,但这不应该有什么区别,对吗

以下是我当前的代码:

public class DragBehavior : Behavior<UIElement>
{
    private const double DRAG_DISTANCE = 20;
    private const int DRAG_ICON_HEIGHT = 100;

    Point m_firstPoint;
    private bool m_isDragging = false;
    private Popup m_dragPopup = null;
    private Image m_draggedImage;        

    protected override void OnAttached()
    {
        this.AssociatedObject.MouseLeftButtonDown += new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown);
        this.AssociatedObject.MouseMove += new MouseEventHandler(AssociatedObject_MouseMove);
        this.AssociatedObject.MouseLeftButtonUp += new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonUp);

        base.OnAttached();
    }

    void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        m_firstPoint = e.GetPosition(null);
        m_isDragging = true;
    }

    void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
    {
        if (m_isDragging)
        {
            Point currentPosition = e.GetPosition(null);

            if (m_dragPopup == null)
            {
                double deltaX = currentPosition.X - m_firstPoint.X;
                double deltaY = currentPosition.Y - m_firstPoint.Y;

                double movement = Math.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
                if (movement > DRAG_DISTANCE)
                {
                    // Get a screen shot of the element this behaviour is attached to
                    WriteableBitmap elementScreenshot = new WriteableBitmap(AssociatedObject, null);

                    // Create an image out of it
                    m_draggedImage = new Image();
                    m_draggedImage.Height = DRAG_ICON_HEIGHT;
                    m_draggedImage.Stretch = Stretch.Uniform;
                    m_draggedImage.Source = elementScreenshot;
                    m_draggedImage.Opacity = 0.4;

                    // Add the image to the popup
                    m_dragPopup = new Popup();
                    m_dragPopup.Child = m_draggedImage;

                    m_dragPopup.IsOpen = true;
                    m_dragPopup.UpdateLayout();

                    m_dragPopup.HorizontalOffset = currentPosition.X - m_draggedImage.ActualWidth/2;
                    m_dragPopup.VerticalOffset = currentPosition.Y - m_draggedImage.ActualHeight/2;

                    AssociatedObject.CaptureMouse();
                }
            }
            else
            {
                m_dragPopup.HorizontalOffset = currentPosition.X - m_draggedImage.ActualWidth/2;
                m_dragPopup.VerticalOffset = currentPosition.Y - m_draggedImage.ActualHeight/2;
            }
        }
    }

    void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (m_isDragging == true && m_dragPopup != null)
        {
            m_isDragging = false;
            m_dragPopup.IsOpen = false;
            m_dragPopup = null;
        }

        AssociatedObject.ReleaseMouseCapture();

        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
    }

    protected override void OnDetaching()
    {
        this.AssociatedObject.MouseLeftButtonDown -= new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown);
        this.AssociatedObject.MouseMove -= new MouseEventHandler(AssociatedObject_MouseMove);
        this.AssociatedObject.MouseLeftButtonUp -= new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonUp);

        base.OnDetaching();
    }
}
公共类DragBehavior:行为
{
私人常数双拖拽距离=20;
私有常量int拖动图标高度=100;
m_第一点;
私有布尔m_IsDraging=false;
私有弹出菜单m_dragPopup=null;
私人图像m_draggedImage;
受保护的覆盖无效附加()
{
this.AssociatedObject.MouseLeftButtonDown+=新的MouseButtonEventHandler(AssociatedObject\u MouseLeftButtonDown);
this.AssociatedObject.MouseMove+=新的MouseEventHandler(AssociatedObject\u MouseMove);
this.AssociatedObject.MouseLeftButtonUp+=新的MouseButtonEventHandler(AssociatedObject\u MouseLeftButtonUp);
base.onatached();
}
void associated object_MouseLeftButtonDown(对象发送器,MouseButtonEventArgs e)
{
m_firstPoint=e.GetPosition(null);
m_IsDraging=真;
}
无效关联对象\u MouseMove(对象发送方,MouseEventArgs e)
{
如果(m_IsDraging)
{
点currentPosition=e.GetPosition(空);
if(m_dragPopup==null)
{
双deltaX=currentPosition.X-m_firstPoint.X;
双三角=当前位置.Y-m_第一点.Y;
双移动=Math.Sqrt((deltaX*deltaX)+(deltaY*deltaY));
如果(移动>拖动距离)
{
//获取此行为附加到的元素的屏幕截图
WriteableBitmap元素屏幕截图=新的WriteableBitmap(关联对象,空);
//从中创建一个图像
m_draggedImage=新图像();
m_draggedImage.Height=拖动图标高度;
m_draggedImage.Stretch=Stretch.Uniform;
m_draggedImage.Source=element屏幕截图;
m_draggedImage.Opacity=0.4;
//将图像添加到弹出窗口
m_dragPopup=新弹出窗口();
m_dragPopup.Child=m_draggedImage;
m_dragPopup.IsOpen=真;
m_dragPopup.UpdateLayout();
m_dragPopup.HorizontalOffset=currentPosition.X-m_draggedImage.ActualWidth/2;
m_dragPopup.VerticalOffset=currentPosition.Y-m_draggedImage.ActualHeight/2;
AssociatedObject.CaptureMouse();
}
}
其他的
{
m_dragPopup.HorizontalOffset=currentPosition.X-m_draggedImage.ActualWidth/2;
m_dragPopup.VerticalOffset=currentPosition.Y-m_draggedImage.ActualHeight/2;
}
}
}
void associated object_MouseLeftButtonUp(对象发送器,MouseButtonEventArgs e)
{
if(m_isDragging==true&&m_dragPopup!=null)
{
m_IsDraging=错误;
m_dragPopup.IsOpen=假;
m_dragPopup=null;
}
AssociatedObject.ReleaseMouseCapture();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
附加时受保护的覆盖无效()
{
this.AssociatedObject.MouseLeftButtonDown-=新的MouseButtonEventHandler(AssociatedObject\u MouseLeftButtonDown);
this.AssociatedObject.MouseMove-=新的MouseEventHandler(AssociatedObject\u MouseMove);
this.AssociatedObject.MouseLeftButtonUp-=新的MouseButtonEventHandler(AssociatedObject\u MouseLeftButtonUp);
base.OnDetaching();
}
}
我知道我看过两篇关于这个的帖子

这一个-forums.silverlight.net/forums/p/142038/317146.aspx没有帮助,因为我尝试强制GC无效,而这一个-我没有真正理解他们的解释,因为他们声称是UIElement与导致它的行为相关联,但是当根引用UIElement被破坏时,对该行为的根引用也将被删除,因此这两个行为都可以被GC替换

我希望这将是简单的,但如果不是,我将开始与WinDbg,看看到底发生了什么

非常感谢您的帮助!:)

谢谢


Andy。

您不显示从何处调用分离。需要实际调用object.Detach()(DragBehavior)才能调用OnDetaching()方法

如果您想在AssociatedObject\u MouseLeftButtonUp事件处理程序中分离它,您可以在方法末尾调用detach,只要您真的不需要对它做更多的操作

void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
{ 
    if (m_isDragging == true && m_dragPopup != null) 
    { 
        m_isDragging = false; 
        m_dragPopup.IsOpen = false; 
        m_dragPopup = null; 
    } 

    AssociatedObject.ReleaseMouseCapture(); 

    this.Detach()
} 

我肯定不会给GC打任何可能会对性能产生巨大影响的电话。

没有人能帮忙吗?我想我会尝试在silverlight论坛上定位!如果我有进展,我会更新这个线程!我想这就是他要表达的观点。当视图从可视树中删除时,Silverlight应该调用“分离”。