C# Avalonia 15- Animation- XamlAnimation

news/2025/11/16 14:48:48/文章来源:https://www.cnblogs.com/dalgleish/p/19096614

同样用两种方式实现动画,各位自行选择。实现了一个ArithmeticConverter类。

ArithmeticConverter.cs类

using Avalonia.Data.Converters;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Shares.Avalonia
{public class ArithmeticConverter : IValueConverter{public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture){if (value == null) return 0.0;if (!double.TryParse(value.ToString(), out double input)) return value;if (parameter == null) return input;string[] ops = parameter.ToString()!.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);foreach (var op in ops){input = ApplyOperation(input, op.Trim());}return input;}public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture){if (value == null) return 0.0;if (!double.TryParse(value.ToString(), out double input)) return value;if (parameter == null) return input;string[] ops = parameter.ToString()!.Split(new[] { ',', ';',':'}, StringSplitOptions.RemoveEmptyEntries);// 倒序执行逆运算for (int i = ops.Length - 1; i >= 0; i--){input = ApplyOperationBack(input, ops[i].Trim());}return input;}private double ApplyOperation(double input, string op){try{if (op.StartsWith("+")) return input + double.Parse(op[1..]);if (op.StartsWith("-")) return input - double.Parse(op[1..]);if (op.StartsWith("*")) return input * double.Parse(op[1..]);if (op.StartsWith("/")) return double.Parse(op[1..]) != 0 ? input / double.Parse(op[1..]) : 0;if (op.StartsWith("^")){double power = double.Parse(op[1..]);// 负数的非整数幂检查if (input < 0 && power % 1 != 0){Console.WriteLine($"警告:负数底数 {input} 与非整数指数 {power} 会产生 NaN");return double.NaN;}return Math.Pow(input, power);}if (op == "√") return input >= 0 ? Math.Sqrt(input) : double.NaN;if (op == "abs") return Math.Abs(input);if (op == "neg") return -input;if (op == "floor") return Math.Floor(input);if (op == "ceil") return Math.Ceiling(input);if (op == "round") return Math.Round(input);// 三角函数(度数模式)if (op == "sin") return Math.Sin(input * Math.PI / 180);if (op == "cos") return Math.Cos(input * Math.PI / 180);if (op == "tan"){double angleMod = input % 180;if (Math.Abs(angleMod - 90) < 1e-10){Console.WriteLine($"警告:tan({input}) 接近 90 + k*180 度奇点");return double.NaN;}return Math.Tan(input * Math.PI / 180);}if (op == "asin"){if (input < -1 || input > 1){Console.WriteLine($"警告:asin({input}) 超出定义域 [-1,1]");return double.NaN;}return Math.Asin(input) * 180 / Math.PI;}if (op == "acos"){if (input < -1 || input > 1){Console.WriteLine($"警告:acos({input}) 超出定义域 [-1,1]");return double.NaN;}return Math.Acos(input) * 180 / Math.PI;}if (op == "atan") return Math.Atan(input) * 180 / Math.PI;// 对数与指数if (op == "ln") return input > 0 ? Math.Log(input) : double.NaN;if (op.StartsWith("log")){double baseVal = 10;if (op.Length > 3) double.TryParse(op[3..], out baseVal);return input > 0 ? Math.Log(input, baseVal) : double.NaN;}if (op == "exp") return Math.Exp(input);}catch (Exception ex){Console.WriteLine($"警告:ApplyOperation错误信息为{ex.Message}");}return input;}private double ApplyOperationBack(double input, string op){try{if (op.StartsWith("+")) return input - double.Parse(op[1..]);if (op.StartsWith("-")) return input + double.Parse(op[1..]);if (op.StartsWith("*")){double factor = double.Parse(op[1..]);if (factor != 0) return input / factor;}if (op.StartsWith("/")){double factor = double.Parse(op[1..]);return input * factor;}if (op.StartsWith("^")){double power = double.Parse(op[1..]);if (input < 0 && power % 1 != 0){Console.WriteLine($"警告:负数底数 {input} 与非整数指数 {power} 会产生 NaN");return double.NaN;}if (power != 0) return Math.Pow(input, 1 / power);}if (op == "√") return input * input;if (op == "neg") return -input;if (op == "abs" || op == "floor" || op == "ceil" || op == "round"){Console.WriteLine($"警告:操作 '{op}' 不可逆,返回当前值");return input;}// 三角函数逆运算(度数模式)if (op == "sin") return Math.Asin(input) * 180 / Math.PI;if (op == "cos") return Math.Acos(input) * 180 / Math.PI;if (op == "tan"){double angleRad = Math.Atan(input);double angleDeg = angleRad * 180 / Math.PI;return angleDeg;}if (op == "asin") return Math.Sin(input * Math.PI / 180);if (op == "acos") return Math.Cos(input * Math.PI / 180);if (op == "atan") return Math.Tan(input * Math.PI / 180);// 对数与指数逆运算if (op == "ln") return Math.Exp(input);if (op.StartsWith("log")){double baseVal = 10;if (op.Length > 3) double.TryParse(op[3..], out baseVal);return Math.Pow(baseVal, input);}if (op == "exp") return Math.Log(input);}catch (Exception ex){Console.WriteLine($"警告:ApplyOperationBack错误信息为{ex.Message}");}return input;}}
}

 XamlAnimation.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="300" Width="300"x:Class="AvaloniaUI.XamlAnimation"Title="XamlAnimation" Name="window"><Window.Resources><ArithmeticConverter x:Key="converter"/></Window.Resources><Window.Styles><Style Selector="Button.Animation1"><Setter Property="Height" Value="40"/><Setter Property="Width" Value="160"/><Style Selector="^:pointerover"><Style.Animations><Animation Duration="0:0:5" FillMode="None"><KeyFrame Cue="100%"><Setter Property="Width" Value="{Binding #window.Width, Converter={StaticResource converter}, ConverterParameter=-100}"/><Setter Property="Height" Value="{Binding #window.Height, Converter={StaticResource converter}, ConverterParameter=-100;/3}"/></KeyFrame></Animation></Style.Animations></Style></Style><Style Selector="Button.Animation2"><Setter Property="Height" Value="40"/><Setter Property="Width" Value="160"/><Setter Property="Transitions"><Transitions><DoubleTransition Property="Width" Duration="0:0:5"/><DoubleTransition Property="Height" Duration="0:0:5"/></Transitions></Setter><Style Selector="^:pointerover"><Setter Property="Width" Value="{Binding #window.Width, Converter={StaticResource converter}, ConverterParameter=-100}"/><Setter Property="Height" Value="{Binding #window.Height, Converter={StaticResource converter}, ConverterParameter=-100;/3}"/></Style></Style></Window.Styles><Button Padding="10" Classes="Animation2"HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"Content="Pointer over and Make Me Grow"></Button>
</Window>

XamlAnimation.axaml.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;namespace AvaloniaUI;public partial class XamlAnimation : Window
{public XamlAnimation(){InitializeComponent();}
}

运行效果

image

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/161292.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

为什么Claude Code放弃代码索引,使用50年前的grep技术

为什么Claude Code放弃代码索引,使用50年前的grep技术? https://mp.weixin.qq.com/s/Fa15GoM3_2CUnjdHQ3I7Nw 为什么Claude Code放弃代码索引,使用50年前的grep技术? 原创 余志臻 腾讯云开发者2025年09月16日 08:45 北京 👉目录1 引言:一个看似倒退的选择2 理解状态的本…

【QT】使用QT编写一款自己的串口助手

步骤一:ui界面设计 控件 接收框控件: (需要将接收框设置为只读 read only) 属性选择控件: 发送框控件: 按钮控件: 外框控件: 文本控件: 界面设计 最终设计结果布局解析程序设计 第一步 在.pro文件中修改增加: QT += core gui serialport // 增加 serialp…

一句话让AI帮你搞营销?火山引擎Data Agent说:这事儿可以的~

本文为火山引擎数据产品总监刘峰的演讲分享,介绍了在过去的半年中,火山引擎Data Agent在智能营销Agent领域的一些新思考、新能力以及落地实践。 各位线上的朋友,大家好!今天主要跟大家聊聊四个关键主题: 首先,面对用户和业务增长挑战,结合大模型我们有哪些新解法? 其次…

网络安全反模式:无效工作生成器的根源与解决方案

本文深入探讨网络安全中常见的“无效工作生成器”反模式,通过真实案例解析其成因,并提出基于根本原因分析的解决方案,包括优先级策略、机制构建及实际应用案例,帮助团队避免资源浪费并提升安全效能。网络安全反模式:无效工作生成器 2025年4月19日 3025字 15分钟阅读 本文…

Excel处理控件Aspose.Cells教程:如何将Excel区域转换为Python列表

在 Python 中处理 Excel 数据通常需要将特定的行和列提取为列表格式。在本教程中,我们将逐步学习如何借助Aspose.Cells在 Python 中将定义的 Excel 范围转换为列表。在 Python 中处理 Excel 数据通常需要将特定的行和列提取为列表格式。将 Excel 范围转换为 Python 列表对于以…

运筹学

运筹学绪论 运筹学主要分支划分方法:一般数学规划模型/特定问题的数学模型。前者划分出线性规划/整数规划/非线性规划/DP/网络流/...,后者划分出网络计划/排队论/存储论/决策论/对策论/... 人工智能的许多问题均与运筹学密不可分 核心:建模与求解 e.g.线性规划:给定基函数和…

国产化Excel开发组件Spire.XLS教程:使用 Python 设置 Excel 格式,从基础到专业应用

与在 Excel 中手动调整样式相比,使用 Python 进行 Excel 格式设置 并自动化操作,能够帮助我们节省宝贵的时间。本文将演示如何借助 Spire.XLS for Python 通过 Python 设置 Excel 样式。 在处理电子表格时,数据本身固然重要,但可读性同样关键。一个拥有优秀格式设置的 Exc…

(附源码)高校拼车管理系统的设计与实现 - 实践

(附源码)高校拼车管理系统的设计与实现 - 实践pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", monospace …

函数计算的云上计费演进:从请求驱动到价值驱动,助力企业走向 AI 时代

函数计算的演进史,其实也是一部计费方式的演化史。透过计费这一窗口,我们可以一管窥全豹,清晰地看到背后产品形态在技术与体验上的深刻变化,以及技术架构随应用场景不断演化的能力。作者:砥行 在云计算的发展过程中,计费方式往往是开发者最直观的感知。最初,用户需要直接…

彩笔运维勇闯机器学习--梯度下降法

前言 彩笔运维勇闯机器学习,今天我们来讨论一下梯度下降法 梯度 首先要搞明白什么是梯度,那就要先从导数说起 导数 函数\(y=f(x)\)的自变量\(x\)在一点\(x_0\)上产生一个增量\(\Delta x\)时,函数输出值的增量\(\Delta y=f(x_0 + \Delta x)-f(x_0)\)与自变量增量\(\Delta x\)…

vs code运行Java遇到的输入问题

关于在vs code中运行Java无法输入鸣谢我的室友徐同学和亲爱的元宝同学还有ChatGPT老师为什么 code runner内置的编译逻辑是直接运行你的代码,但是java的独特输入方式正好与其不同,导致直接默认输入为空 public class Sqrt{public static void main(String[] args) {double EP…

关于数据跨境,你应该了解的合规难题有哪些?

数据跨境合规难破?匿名化就丢数据价值?本文详解如何攻克隐私保护与算法研发的矛盾,从精准模糊到生成式AI匿名化技术,助你合规传输高价值数据,释放全球研发潜能!当下正是一个由数据驱动的伟大变革时代。从ADAS到AD,每一次技术的跃迁都离不开海量道路数据的采集、标注与分…

doubletrouble wp复盘

因为这台机子形式比较特殊,所以做个wp nmap ┌──(kali㉿kali)-[~/replay/doubletr] └─$ nmap -sT -p- 192.168.48.67 Starting Nmap 7.95 ( https://nmap.org ) at 2025-09-10 23:17 EDT Nmap scan report for 192.168.48.67 Host is up (0.0058s latency). Not shown: 6…

软件设计师知识点总结(2023)上

第1题: 第2题: 第3题: 第4题: 第5题: 第6题:第7题:第8题: 第9题: 先来先服务:最短寻道时间:电梯调度: 单向扫描: 第10题: 第11题: 第12题: 第13题: 第14题: 第15题: 第16题: 第17题: 第18题: 第19题:

【运维自动化-标准运维】各类全局变量使用说明(中)

一、集群资源筛选 此变量用于按照资源筛选方案创建新的集群。 创建 输入名称和KEY值 引用 ${KEY}引用${KEY},返回的是创建集群成功的信息Allocate {set_number} sets with names: 引用${KEY._module},返回的是集群下的模块信息,类型为字典,键为模块名,值为模块下的主机列表…

adobe illustrator中生成连续直角线段

001、工具栏选矩形工具 002、绘制一个矩形 003、鼠标切换到这里 004、 鼠标以横向拖动的方式选中一个边 005、键盘delete健删除 006、同样的方法删除下边这条边 。

【IEEE出版|EI检索稳定】第四届云计算、大数据应用与软件工程国际学术会议(CBASE 2025)

CBASE 2025旨在汇聚全球云计算、大数据、软件工程等计算机相关领域的顶尖学者与专业人士,打造一个高水平、国际化的学术交流平台,共同探讨最新研究进展与发展趋势。会议诚挚欢迎云计算、大数据、软件工程、网络安全、人工智能、计算机视觉、机器学习、智能计算等方向的专家学…

取证 - voasem

分类 内存取证 经常利用volatility分析取证文件后缀 .raw、.vmem、.img 常用命令(imageinfo,pslist,dumpfiles,memdump) 可疑的进程(notepad,cmd) 和磁盘取证结合起来考察 了解部分操作系统原理 常见文件后缀dmg,img命令 python3 vol.py -f [取证文件] 插件可以使用 -h…

Symbol VBRK: Invalid data type u SAP 事务成功新号码获取到 但是提交后提示失败如何处理

前几天用户提了个错误问题 Symbol "VBRK": Invalid data type "u" 提示说发票创建车工但是离开的时候报错 分析问题可能 增强失败 ST22检查没有讯息 今天经过更老的老师傅提示 SM14查看可能事务回滚了 赶紧查看了下 有错 点击 DEBUG发现是发票创建成功以…

three.js中怎么加载 .gltf/glb格式 文件

3D编辑器格式 用于特定应用程序(主要是3D编辑器):. .blend (Blender), .max (3d Studio Max), .mb and .ma (Maya), etc... 交换格式 有.OBJ, .DAE (Collada), .FBX.等格式。它们被设计出来用于3D编辑器之间交换信息的。因此,它们通常比所需的大得多(内含3D编辑器内所需要的…