RawTouch.axaml代码

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Height="363" Width="568"
        x:Class="AvaloniaUI.RawTouch"
        Title="RawTouch">
    <Grid>
        <Canvas
            x:Name="canvas" Background="LightSkyBlue"
            PointerPressed="canvas_TouchDown" PointerReleased="canvas_TouchUp"
            PointerMoved="canvas_TouchMove">
        </Canvas>
    </Grid>
</Window>

RawTouch.axaml.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using System;
using System.Collections.Generic;
using System.Drawing;

namespace AvaloniaUI;

public partial class RawTouch : Window
{
    public RawTouch()
    {
        InitializeComponent();
    }
    // Store the active ellipses, each of which corresponds to a place the user is currently touching down.
    private Dictionary<int, Ellipse> movingEllipses = new Dictionary<int, Ellipse>();

    private void canvas_TouchDown(object? sender, PointerPressedEventArgs e)
    {
        if (sender is Canvas canvas)
        {
            // Create an ellipse to draw at the new touch-down point.
            Ellipse ellipse;
            ellipse = new Ellipse();
            ellipse.Width = 30;
            ellipse.Height = 30;
            ellipse.Stroke = Brushes.White;
            ellipse.Fill = Brushes.Green;

            // Position the ellipse at the touch-down point.
            var touchPoint = e.GetPosition(canvas);
            Canvas.SetLeft(ellipse, touchPoint.X - ellipse.Width / 2);
            Canvas.SetTop(ellipse, touchPoint.Y - ellipse.Height / 2);

            // Store the ellipse in the active collection.
            movingEllipses[e.Pointer.Id] = ellipse;

            // Add the ellipse to the Canvas.
            canvas.Children.Add(ellipse);
        }
    }

    private void canvas_TouchMove(object? sender, PointerEventArgs e)
    {
        if (sender is Canvas canvas && movingEllipses.TryGetValue(e.Pointer.Id, out var ellipse))
        {
            // Move it to the new touch-down point.
            var touchPoint = e.GetPosition(canvas);
            Canvas.SetLeft(ellipse, touchPoint.X - ellipse.Width / 2);
            Canvas.SetTop(ellipse, touchPoint.Y - ellipse.Height / 2);
        }
    }

    private void canvas_TouchUp(object? sender, PointerReleasedEventArgs e)
    {
        if (sender is Canvas canvas)
        {
            var id = e.Pointer.Id;
            if (movingEllipses.TryGetValue(id, out var ellipse))
            {
                // 可选:从画布移除
                canvas.Children.Remove(ellipse);
                movingEllipses.Remove(id);
            }

            e.Pointer.Capture(null); // 释放 pointer 捕获
        }
    }
}

运行效果

 

posted on 2025-07-12 13:24  dalgleish  阅读(16)  评论(0)    收藏  举报