[学习记号 - SL代码] Silverlight可拖放工具类

本文介绍了一个用于实现域内多个控件可拖放功能的通用类DragDrop,该类通过捕获鼠标事件并调整控件位置来实现实现。包括加载和卸载该类的方法,以供复用。

解决的问题:对一个域多个控件实现可拖放的鼠标操作,将此实现封装为一个常用类以便复用

public static class DragDrop
{
    private static bool IsDragging = false;
    private static Point curPoint;
    private const int MAX_ZINDEX = 99999;
    private const double CURRENT_OPACITY = 0.5;
    private static int lastZIndex;
    private static double lastOpacity;
    private static void sender_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        UIElement uiElement = sender as UIElement;
        if (uiElement != null)
        {
            uiElement.CaptureMouse();
            lastZIndex = (int)uiElement.GetValue(Canvas.ZIndexProperty);
            uiElement.SetValue(Canvas.ZIndexProperty, MAX_ZINDEX);
            lastOpacity = uiElement.Opacity;
            uiElement.Opacity = CURRENT_OPACITY;
            IsDragging = true;
            curPoint = new Point(e.GetPosition(null).X, e.GetPosition(null).Y);
        }
    }
    private static void sender_MouseMove(object sender, MouseEventArgs e)
    {
        if (!IsDragging)
        {
            return;
        }
        UIElement uiElement = sender as UIElement;
        if (uiElement != null)
        {
            double currentLeft = (double)uiElement.GetValue(Canvas.LeftProperty);
            double currentTop = (double)uiElement.GetValue(Canvas.TopProperty);
            double newLeft = (double)currentLeft + e.GetPosition(null).X - curPoint.X;
            double newTop = (double)currentTop + e.GetPosition(null).Y - curPoint.Y;
            uiElement.SetValue(Canvas.LeftProperty, newLeft);
            uiElement.SetValue(Canvas.TopProperty, newTop);
            curPoint = new Point(e.GetPosition(null).X, e.GetPosition(null).Y);
        }
    }
    private static void sender_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        UIElement uiElement = sender as UIElement;
        if (uiElement != null)
        {
            uiElement.ReleaseMouseCapture();
            IsDragging = false;
            uiElement.SetValue(Canvas.ZIndexProperty, lastZIndex);
            uiElement.Opacity = lastOpacity;
        }
    }
    public static void Load(UIElement sender)
    {
        sender.MouseLeftButtonDown += new MouseButtonEventHandler(sender_MouseLeftButtonDown);
        sender.MouseLeftButtonUp += new MouseButtonEventHandler(sender_MouseLeftButtonUp);
        sender.MouseMove += new MouseEventHandler(sender_MouseMove);
    }
    public static void UnLoad(UIElement sender)
    {
        sender.MouseLeftButtonDown -= new MouseButtonEventHandler(sender_MouseLeftButtonDown);
        sender.MouseLeftButtonUp -= new MouseButtonEventHandler(sender_MouseLeftButtonUp);
        sender.MouseMove -= new MouseEventHandler(sender_MouseMove);
    }
}

 

原文参考:http://bbs.silverlightchina.net/forum.php?mod=viewthread&tid=2143&extra=page%3D2%26filter%3Ddigest%26digest%3D1

先展示下效果 https://pan.quark.cn/s/5061241daffd 在使用Apache HttpClient库发起HTTP请求的过程中,有可能遇到`HttpClient`返回`response`为`null`的现象,这通常暗示着请求未能成功执行或部分资源未能得到妥善处理。 在本文中,我们将详细研究该问题的成因以及应对策略。 我们需要掌握`HttpClient`的运作机制。 `HttpClient`是一个功能强大的Java库,用于发送HTTP请求并接收响应。 它提供了丰富的API,能够处理多种HTTP方法(例如GET、POST等),支持重试机制、连接池管理以及自定义请求头等特性。 然而,一旦`response`对象为`null`,可能涉及以下几种情形:1. **连接故障**:网络连接未成功建立或在请求期间中断。 需要检查网络配置,确保服务器地址准确且可访问。 2. **超时配置**:若请求超时,`HttpClient`可能不会返回`response`。 应检查连接和读取超时设置,并根据实际需求进行适当调整。 3. **服务器故障**:服务器可能返回了错误状态码(如500内部服务器错误),`HttpClient`无法解析该响应。 建议查看服务器日志以获取更多详细信息。 4. **资源管理**:在某些情况下,如果请求的响应实体未被正确关闭,可能导致连接被提前释放,进而使后续的`response`对象为`null`。 在使用`HttpClient 3.x`版本时,必须手动调用`HttpMethod.releaseConnection()`来释放连接。 而在`HttpClient 4.x`及以上版本中,推荐采用`EntityUtils.consumeQuietly(respons...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值