" />

封装avalonia指定组件允许拖动的工具类

封装,avalonia,指定,组件,允许,拖动,工具 · 浏览次数 : 322

小编点评

使用 System.Collections.Concurrent.ConcurrentDictionary静态参数,指定组件为Key ,Value为DragModule ,DragModule模型中定义了拖动的逻辑在调用StartDrag的时候传递需要拖动的组件,他会创建一个DragModule对象,创建的时候会创建定时器,当鼠标被按下时启动定时器,当鼠标被释放时定时器被停止,定时器用于平滑更新窗体移动,如果直接移动窗体会抖动。 using System;using System.Collections.Concurrent;using Avalonia;using Avalonia.Controls;using Avalonia.Input;using Avalonia.Threading;using Avalonia.VisualTree;namespace DragDemo;public class DragControlHelper{ private static ConcurrentDictionary<Control, DragModule> _dragModules = new(); public static void StartDrag(Control userControl) { _dragModules.TryAdd(userControl, new DragModule(userControl)); } public static void StopDrag(Control userControl) { if (_dragModules.TryRemove(userControl, out var dragModule)) { dragModule.Dispose(); } }class DragModule : IDisposable{ /// <summary> /// 记录上一次鼠标位置 /// </summary> private Point? lastMousePosition; /// <summary> /// 用于平滑更新坐标的计时器 /// </summary> private DispatcherTimer _timer; /// <summary> /// 标记是否先启动了拖动 /// </summary> private bool isDragging = false; /// <summary> /// 需要更新的坐标点 /// </summary> private PixelPoint? _targetPosition; public Control UserControl { get; set; } public DragModule(Control userControl) { UserControl = userControl; // 添加当前控件的事件,用于监听鼠标移动事件 userControl.Events.PointerPressed += (e) => { if (!e.GetCurrentPoint(UserControl).Properties.IsLeftButtonPressed) return; isDragging = true; lastMousePosition = e.GetPosition(UserControl); e.Handled = true; }; userControl.Events.PointerReleased += (e) => { if (!isDragging) return; isDragging = false; e.Handled = true; }; // 启动计时器 _timer.Start(); } private void OnPointerPressed(object sender, PointerPressedEventArgs e) { if (!e.GetCurrentPoint(UserControl).Properties.IsLeftButtonPressed) return; // 启动拖动 isDragging = true; // 记录当前坐标 lastMousePosition = e.GetPosition(UserControl); e.Handled = true; } private void OnPointerReleased(object sender, PointerReleasedEventArgs e) { if (!isDragging) return; // 停止拖动 isDragging = false; e.Handled = true; } private void OnPointerMoved(object sender, PointerEventArgs e) { if (!e.GetCurrentPoint(UserControl).Properties.IsLeftButtonPressed) return; // 如果没有启动拖动,则不执行 if (!isDragging) return; var currentMousePosition = e.GetPosition(UserControl); var offset =currentMousePosition - lastMousePosition.Value; var window = UserControl.FindAncestorOfType<Window>(); if (window != null) { // 记录当前坐标 _targetPosition = new PixelPoint(window.Position.X + (int)offset.X, window.Position.Y + (int)offset.Y); } } public void Dispose() { _timer.Stop(); _targetPosition = null; lastMousePosition = null; }}打开MainWindow.axaml.cs,修改成以下代码 ,在渲染成功以后拿到Border(需要移动的组件),添加到DragControlHelper.StartDrag(border);中,然后再OnUnloaded的时候将Border再卸载掉using Avalonia;using Avalonia.Controls;using Avalonia.Media;using Avalonia.Threading;namespace DragDemo.Views;public partial class MainWindow : Window{ public MainWindow() { InitializeComponent(); this.TransparencyLevelHint = WindowTransparencyLevel.Transparent; ExtendClientAreaToDecorationsHint = true; WindowState = WindowState.Maximized; } public override void Render(DrawingContext context) { base.Render(context); Dispatcher.UIThread.Post(() => { var border = this.Find<Border>(\"Border\"); DragControlHelper.StartDrag(border); }); } protected override void OnUnloaded() { var border = this.Find<Border>(\"Border\"); DragControlHelper.StopDrag(border); base.OnUnloaded(); }}

正文

封装avalonia指定组件允许拖动的工具类

创建Avalonia的MVVM项目,命名DragDemo ,然后将项目的Nuget包更新到预览版

    <ItemGroup>
        <PackageReference Include="Avalonia" Version="11.0.0-preview5" />
        <PackageReference Include="Avalonia.Desktop" Version="11.0.0-preview5" />
        <!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
        <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.0-preview5" />
        <PackageReference Include="Avalonia.ReactiveUI" Version="11.0.0-preview5" />
        <PackageReference Include="XamlNameReferenceGenerator" Version="1.5.1" />
    </ItemGroup>

更新完成以后ViewLocatorApp.axaml会报错,

修改ViewLocator.cs为下面的代码

using System;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using DragDemo.ViewModels;

namespace DragDemo;

public class ViewLocator : IDataTemplate
{
    /// <summary>
    /// 将IControl修改成Control
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public Control Build(object data)
    {
        var name = data.GetType().FullName!.Replace("ViewModel", "View");
        var type = Type.GetType(name);

        if (type != null)
        {
            return (Control)Activator.CreateInstance(type)!;
        }

        return new TextBlock { Text = "Not Found: " + name };
    }

    public bool Match(object data)
    {
        return data is ViewModelBase;
    }
}

添加Avalonia.Themes.Fluent,因为预览版本的包已经独立需要单独安装

<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.0-preview5" />

打开App.axaml文件,修改为以下代码

<Application xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="using:DragDemo"
             RequestedThemeVariant="Light" 
             x:Class="DragDemo.App">
    <Application.DataTemplates>
        <local:ViewLocator/>
    </Application.DataTemplates>

    <Application.Styles>
        <FluentTheme DensityStyle="Compact"/>
    </Application.Styles>
    
</Application>

打开Views/MainWindow.axaml

在头部添加以下代码,让窗口无边框,设置指定窗口Height="38" Width="471",参数让其不要占用整个屏幕,

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:DragDemo.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="DragDemo.Views.MainWindow"
        Icon="/Assets/avalonia-logo.ico"
        ExtendClientAreaToDecorationsHint="True"
        ExtendClientAreaChromeHints="NoChrome"
        ExtendClientAreaTitleBarHeightHint="-1"
        MaxHeight="38" MaxWidth="471"
        Title="DragDemo">
    <Window.Styles>
        <Style Selector="Window">
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
        </Style>
    </Window.Styles>
    <Design.DataContext>
        <vm:MainWindowViewModel/>
    </Design.DataContext>
    
    <StackPanel>
        <StackPanel Opacity="0.1" Height="38" Width="471">
        </StackPanel>
        <Border Name="Border" Width="471" CornerRadius="10" Opacity="1" Background="#FFFFFF">
            <Button>按钮</Button>    
        </Border>
    </StackPanel>
</Window>

以下代码在上面窗口用于设置窗口无边框

    <Window.Styles>
        <Style Selector="Window">
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
        </Style>
    </Window.Styles>

然后打开/Views/MainWindow.axaml.cs文件,将边框设置成无边框,并且设置窗体透明为WindowTransparencyLevel.Transparent

using Avalonia;
using Avalonia.Controls;

namespace DragDemo.Views;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.TransparencyLevelHint = WindowTransparencyLevel.Transparent;
        ExtendClientAreaToDecorationsHint = true;
        WindowState = WindowState.Maximized;
    }
}

效果图如下,因为限制了窗体最大大小,并且在按钮上面添加了透明区块,这样看起来就像是悬浮了

image-20230228193242400

然后我们开始写指定组件拖动工具类,创建DragControlHelper.cs 以下就是封装的工具类 定义了一个ConcurrentDictionary静态参数,指定组件为KeyValueDragModuleDragModule模型中定义了拖动的逻辑在调用StartDrag的时候传递需要拖动的组件,他会创建一个DragModule对象,创建的时候会创建定时器,当鼠标被按下时启动定时器,当鼠标被释放时定时器被停止,定时器用于平滑更新窗体移动,如果直接移动窗体会抖动。

using System;
using System.Collections.Concurrent;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Threading;
using Avalonia.VisualTree;

namespace DragDemo;


public class DragControlHelper
{
    private static ConcurrentDictionary<Control, DragModule> _dragModules = new();

    public static void StartDrag(Control userControl)
    {
        _dragModules.TryAdd(userControl, new DragModule(userControl));
    }

    public static void StopDrag(Control userControl)
    {
        if (_dragModules.TryRemove(userControl, out var dragModule))
        {
            dragModule.Dispose();
        }
    }
}

class DragModule : IDisposable
{
    /// <summary>
    /// 记录上一次鼠标位置
    /// </summary>
    private Point? lastMousePosition;

    /// <summary>
    /// 用于平滑更新坐标的计时器
    /// </summary>
    private DispatcherTimer _timer;

    /// <summary>
    /// 标记是否先启动了拖动
    /// </summary>
    private bool isDragging = false;

    /// <summary>
    /// 需要更新的坐标点
    /// </summary>
    private PixelPoint? _targetPosition;

    public Control UserControl { get; set; }

    public DragModule(Control userControl)
    {
        UserControl = userControl;
        // 添加当前控件的事件监听
        UserControl.PointerPressed += OnPointerPressed;
        UserControl.PointerMoved += OnPointerMoved;
        UserControl.PointerReleased += OnPointerReleased;

        // 初始化计时器
        _timer = new DispatcherTimer
        {
            Interval = TimeSpan.FromMilliseconds(10)
        };
        _timer.Tick += OnTimerTick;
    }


    /// <summary>
    /// 计时器事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void OnTimerTick(object sender, EventArgs e)
    {
        var window = UserControl.FindAncestorOfType<Window>();
        if (window != null && window.Position != _targetPosition)
        {
            // 更新坐标
            window.Position = (PixelPoint)_targetPosition;
        }
    }

    private void OnPointerPressed(object sender, PointerPressedEventArgs e)
    {
        if (!e.GetCurrentPoint(UserControl).Properties.IsLeftButtonPressed) return;
        // 启动拖动
        isDragging = true;
        // 记录当前坐标
        lastMousePosition = e.GetPosition(UserControl);
        e.Handled = true;
        // 启动计时器
        _timer.Start();
    }

    private void OnPointerReleased(object sender, PointerReleasedEventArgs e)
    {
        if (!isDragging) return;
        // 停止拖动
        isDragging = false;
        e.Handled = true;
        // 停止计时器
        _timer.Stop();
    }

    private void OnPointerMoved(object sender, PointerEventArgs e)
    {
        if (!e.GetCurrentPoint(UserControl).Properties.IsLeftButtonPressed) return;

        // 如果没有启动拖动,则不执行
        if (!isDragging) return;

        var currentMousePosition = e.GetPosition(UserControl);
        var offset =currentMousePosition - lastMousePosition.Value;
        var window = UserControl.FindAncestorOfType<Window>();
        if (window != null)
        {
            // 记录当前坐标
            _targetPosition = new PixelPoint(window.Position.X + (int)offset.X,
                window.Position.Y + (int)offset.Y);
        }
    }

    public void Dispose()
    {
        _timer.Stop();
        _targetPosition = null;
        lastMousePosition = null;
    }
}

打开MainWindow.axaml.cs,修改成以下代码 ,在渲染成功以后拿到Border(需要移动的组件),添加到DragControlHelper.StartDrag(border);中,然后再OnUnloaded的时候将Border再卸载掉

using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Threading;

namespace DragDemo.Views;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.TransparencyLevelHint = WindowTransparencyLevel.Transparent;
        ExtendClientAreaToDecorationsHint = true;
        WindowState = WindowState.Maximized;
    }

    public override void Render(DrawingContext context)
    {
        base.Render(context);
        Dispatcher.UIThread.Post(() =>
        {
            var border = this.Find<Border>("Border");
            DragControlHelper.StartDrag(border);
        });
    }

    protected override void OnUnloaded()
    {
        var border = this.Find<Border>("Border");
        DragControlHelper.StopDrag(border);
        base.OnUnloaded();
    }
}

效果展示:

来着token的分享

技术交流群:737776595

与封装avalonia指定组件允许拖动的工具类相似的内容:

封装avalonia指定组件允许拖动的工具类

封装avalonia指定组件允许拖动的工具类 创建Avalonia的MVVM项目,命名DragDemo ,然后将项目的Nuget包更新到预览版

我封装的一个REPR轮子 Biwen.QuickApi

Biwen.QuickApi 项目介绍 [QuickApi("hello/world")] public class MyApi : BaseQuickApi{} 提供一种简单集成的Minimal Web Api交互模块 遵循了 REPR 设计 (Request-Endpoint-

springboot~封装依赖引用包jar还是pom,哪种更规范

将多个第三方包封装成一个项目后,如果你的目的是让其他开发人员可以直接引用这些依赖,一般来说有两种常见的方式: 打成JAR包:将封装好的项目编译打包成JAR文件,其他开发人员可以将这个JAR文件添加到他们的项目中,并在项目的构建工具(比如Maven)中配置该JAR作为依赖。这样做的好处是简单直接,其他

validator库在gin中的使用

目录封装语言包翻译器tag中设置验证规则控制层验curl请求返回结果 封装语言包翻译器 package validator import ( "fmt" "net/http" "reflect" "github.com/go-playground/locales/zh_Hans_CN" unTran

密钥封装和公钥加密的联系和区别?

密钥封装和公钥加密的联系和区别? 转载&学习:https://www.zhihu.com/question/443779639 先理解下面这句话: 密钥封装机制是面向的互联网应用,最大的用处是网络连接时建立双方的临时会话密钥。既然是应用就要考虑到网络传输的协议,协议支持的包大小是有限制的,如果公钥加

6.1 C/C++ 封装字符串操作

C/C++语言是一种通用的编程语言,具有高效、灵活和可移植等特点。C语言主要用于系统编程,如操作系统、编译器、数据库等;C语言是C语言的扩展,增加了面向对象编程的特性,适用于大型软件系统、图形用户界面、嵌入式系统等。C/C++语言具有很高的效率和控制能力,但也需要开发人员自行管理内存等底层资源,对于初学者来说可能会有一定的难度。

用go封装一下二级认证功能

本篇为[用go设计开发一个自己的轻量级登录库/框架吧 - 秋玻 - 博客园 (cnblogs.com)]的二级认证业务篇,会讲讲二级认证业务的实现,给库/框架增加新的功能。 源码:https://github.com/weloe/token-go

vue3 封装axios

1添加一个新的 http.js文件 封装axios 引入axios //引入Axios import axios from 'axios' 定义一个根地址 //视你自己的接口地址而定 var root2 = 'http://121.4.63.196:8520/api' 定义个小函数来统一参数格式(可

Qt-qrencode开发-生成、显示二维码

将qrencode使用Qt封装为一个生成、显示二维码的控件; 支持使用QPainter绘制显示二维码; 可通过函数接口返回生成的二维码QImage; 可通过调用函数将生成的二维码保存到指定路径; 支持使用源码嵌入工程(更好的跨平台、支持各种编译器); 支持使用编译好的静态库(MSVC2017-64)...

基于Vue3水印组件封装:防篡改守护!

基于Vue 3的全新水印通用组件封装。这款组件不仅功能强大,而且易于集成,能够轻松为您在网页任何位置添加自定义水印,有效防止内容被篡改或盗用。