弹来弹去跑马灯!

UWP ManipulationStarted 移动图片或控件不要滑出父容器的判断

假设自定义一个用户控件用以在父容器Grid里拖动/移动:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<UserControl
    x:Class="App6.Pic"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App6"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"  ManipulationMode="All"
    ManipulationStarted="UserControl_ManipulationStarted" ManipulationDelta="UserControl_ManipulationDelta"
    d:DesignHeight="300" RenderTransformOrigin="0.5,0.5"
    d:DesignWidth="400">
    <UserControl.RenderTransform>
        <TranslateTransform x:Name="t" />
    </UserControl.RenderTransform>
    <Grid Background="AliceBlue">
       <TextBlock Name="txt" Text="" />
    </Grid>
</UserControl>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
 
// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236
 
namespace App6
{
    public sealed partial class Pic : UserControl
    {
        public Pic()
        {
            this.InitializeComponent();
             
        }
 
        private void UserControl_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
        {
            
 
        }
 
        private void UserControl_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
 
            this.t.X += e.Delta.Translation.X;
            this.t.Y += e.Delta.Translation.Y;
            txt.Text = "" + (Parent as Grid).ActualWidth + ",  " + t.X + this.Margin.Left +  this.ActualWidth / 2;
            //判断核设置移动范围限制在父容器内
            if ((Parent as Grid).ActualWidth < this.Margin.Left+ t.X + this.Width)//to right
            {
                this.t.X = (Parent as Grid).ActualWidth - (this.Margin.Left + this.Width);
            }
 
            if (0 >this.Margin.Left + t.X )//to left
            {
                this.t.X =  - this.Margin.Left ;
            }
 
            if (0 > this.Margin.Top + t.Y)//to top
            {
                this.t.Y= -this.Margin.Top;
            }
 
            if ((Parent as Grid).ActualHeight < this.Margin.Top + t.Y + this.Height)//to bottom
            {
                this.t.Y = (Parent as Grid).ActualHeight - (this.Margin.Top + this.Height);
            }
 
 
        }
 
 
 
    }
}

  

 

posted @   wgscd  阅读(71)  评论(0编辑  收藏  举报
编辑推荐:
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· 程序员常用高效实用工具推荐,办公效率提升利器!
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
点击右上角即可分享
微信分享提示