当前位置: 首页 > news >正文

C# Avalonia 18- ControlTemplates - FlipPanelTest

FlipPanel2类是负责控制逻辑。

FlipPanel2.cs

using Avalonia;
using Avalonia.Animation.Easings;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using System;namespace AvaloniaUI
{[TemplatePart("PART_FrontHost", typeof(Border))][TemplatePart("PART_BackHost", typeof(Border))][TemplatePart("PART_FlipButton", typeof(ToggleButton))]public class FlipPanel2 : TemplatedControl{// ========== 依赖属性 ==========public static readonly StyledProperty<Control?> FrontContentProperty =AvaloniaProperty.Register<FlipPanel2, Control?>(nameof(FrontContent));public static readonly StyledProperty<Control?> BackContentProperty =AvaloniaProperty.Register<FlipPanel2, Control?>(nameof(BackContent));public static readonly StyledProperty<bool> IsFlippedProperty =AvaloniaProperty.Register<FlipPanel2, bool>(nameof(IsFlipped));public static readonly StyledProperty<double> FlipAngleProperty =AvaloniaProperty.Register<FlipPanel2, double>(nameof(FlipAngle), 0d);// ========== CLR 包装 ==========public Control? FrontContent{get => GetValue(FrontContentProperty);set => SetValue(FrontContentProperty, value);}public Control? BackContent{get => GetValue(BackContentProperty);set => SetValue(BackContentProperty, value);}public bool IsFlipped{get => GetValue(IsFlippedProperty);set => SetValue(IsFlippedProperty, value);}public double FlipAngle{get => GetValue(FlipAngleProperty);set => SetValue(FlipAngleProperty, value);}// ========== 模板部件 ==========private ToggleButton? flipButton;public FlipPanel2(){// 监听属性变化this.GetObservable(IsFlippedProperty).Subscribe(_ =>{UpdatePseudoClasses();});}protected override void OnApplyTemplate(TemplateAppliedEventArgs e){base.OnApplyTemplate(e);flipButton = e.NameScope.Find<ToggleButton>("PART_FlipButton");if (flipButton != null)flipButton.Click += (_, _) => ToggleFlipped();UpdatePseudoClasses();}private void ToggleFlipped(){IsFlipped = !IsFlipped;}private void UpdatePseudoClasses(){PseudoClasses.Set(":flipped", IsFlipped);}}
}

测试的Style.axaml

<Styles xmlns="https://github.com/avaloniaui"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:AvaloniaUI"><Design.PreviewWith></Design.PreviewWith><Style Selector="local|FlipPanel2"><!-- 默认角度 --><Setter Property="local:FlipPanel2.FlipAngle" Value="0"/><Setter Property="Template"><Setter.Value><ControlTemplate><Grid RowDefinitions="*, auto"><!-- Front content --><Border x:Name="PART_FrontHost"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"CornerRadius="{TemplateBinding CornerRadius}"Background="#E0F7FA"Opacity="1"><ContentPresenter Content="{TemplateBinding FrontContent}"HorizontalAlignment="Center"VerticalAlignment="Center"/><Border.Transitions><Transitions><DoubleTransition Property="Opacity"Duration="0:0:0.3"Easing="SineEaseInOut"/></Transitions></Border.Transitions></Border><!-- Back content --><Border x:Name="PART_BackHost"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"CornerRadius="{TemplateBinding CornerRadius}"Background="#FFF3E0"Opacity="0"><ContentPresenter Content="{TemplateBinding BackContent}"HorizontalAlignment="Center"VerticalAlignment="Center"/><Border.Transitions><Transitions><DoubleTransition Property="Opacity"Duration="0:0:0.3"Easing="SineEaseInOut"/></Transitions></Border.Transitions></Border><!-- Flip button --><ToggleButton x:Name="PART_FlipButton"Grid.Row="1"Width="20" Height="20"Margin="0,5,0,5"HorizontalAlignment="Center"VerticalAlignment="Center"><ToggleButton.Template><ControlTemplate><Grid><Ellipse Stroke="#555" Fill="WhiteSmoke" StrokeThickness="0.5"/><Path Data="M4,5 L7,6.5 L4,8 Z" Margin="2,0,0,0"Fill="#455A64"Height="15"Stretch="Uniform"HorizontalAlignment="Center"VerticalAlignment="Center"/></Grid></ControlTemplate></ToggleButton.Template><ToggleButton.RenderTransform><Rotate3DTransform AngleY="{Binding FlipAngle, RelativeSource={RelativeSource TemplatedParent}}"><Rotate3DTransform.Transitions><Transitions><DoubleTransition Property="AngleY"Duration="0:0:0.3"Easing="SineEaseInOut"/></Transitions></Rotate3DTransform.Transitions></Rotate3DTransform></ToggleButton.RenderTransform></ToggleButton></Grid></ControlTemplate></Setter.Value></Setter><!-- flipped 状态样式 --><Style Selector="^:flipped /template/ Border#PART_FrontHost"><Setter Property="Opacity" Value="0"/><Setter Property="IsHitTestVisible" Value="False"/></Style><Style Selector="^:flipped /template/ Border#PART_BackHost"><Setter Property="Opacity" Value="1"/><Setter Property="IsHitTestVisible" Value="True"/></Style><Style Selector="^:not(:flipped) /template/ Border#PART_FrontHost"><Setter Property="IsHitTestVisible" Value="True"/></Style><Style Selector="^:not(:flipped) /template/ Border#PART_BackHost"><Setter Property="IsHitTestVisible" Value="False"/></Style><!-- 旋转角度 --><Style Selector="^:flipped"x:SetterTargetType="local:FlipPanel2"><Setter Property="local:FlipPanel2.FlipAngle" Value="180"/></Style></Style>
</Styles>

FlipPanelTest.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"xmlns:local="using:AvaloniaUI"Height="300" Width="300"x:Class="AvaloniaUI.FlipPanelTest"Title="FlipPanelTest"><Grid Background="Transparent"><local:FlipPanel2 x:Name="panel"BorderBrush="DarkOrange"BorderThickness="3"IsFlipped="True"CornerRadius="4"Margin="10"><!-- Front content --><local:FlipPanel2.FrontContent><StackPanel Margin="6"><StackPanel.Styles><Style Selector="Button"><Setter Property="HorizontalAlignment" Value="Center"/><Setter Property="Padding" Value="3"/><Setter Property="Margin" Value="3"/></Style></StackPanel.Styles><TextBlock Text="This is the front side of the FlipPanel."TextWrapping="Wrap"Margin="3"FontSize="16"Foreground="DarkOrange"/><Button Content="Button One"/><Button Content="Button Two"/><Button Content="Button Three"/><Button Content="Button Four"/></StackPanel></local:FlipPanel2.FrontContent><!-- Back content --><local:FlipPanel2.BackContent><Grid Margin="6" RowDefinitions="auto,*"><TextBlock Text="This is the back side of the FlipPanel."TextWrapping="Wrap"Margin="3"FontSize="16"Foreground="DarkMagenta"/><Button Grid.Row="1"Margin="3"Padding="10"Content="Flip Back to Front"HorizontalAlignment="Center"VerticalAlignment="Center"Click="cmdFlip_Click"/></Grid></local:FlipPanel2.BackContent></local:FlipPanel2></Grid>
</Window>

FlipPanelTest.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Shares.Avalonia;namespace AvaloniaUI;public partial class FlipPanelTest : Window
{public FlipPanelTest(){InitializeComponent();this.Load("avares://AvaloniaUI/Demos/Book/18/CustomControls/FlipPanel.axaml");}private void cmdFlip_Click(object? sender, RoutedEventArgs e){// 切换翻转状态panel.IsFlipped = !panel.IsFlipped;}
}

运行效果

image

 

http://www.gsyq.cn/news/57422.html

相关文章:

  • CTF逆向Re:零基础系统性入门教程-5-动态调试
  • CF1817B Fish Graph
  • 淮安市一对一辅导机构权威排行榜推荐:2026家教机构穿透式测评!
  • 南昌航空大学-软件学院-23207201-吕玉英
  • 从超时到秒杀:三路快排解决数组排序的完整实战与反思
  • 2025年发电机制造厂权威推荐榜单:康姆勒原装发电机组/康姆勒发电机组/全自动柴油发电机组源头厂家精选
  • 2025百元白酒精选推荐指南:十大香型佳酿与纯粮酒挑选策略
  • 部署tendis 集群
  • 完整教程:Flowable工作流引擎:核心表结构概述
  • 2025年刮板蒸发器定做厂家权威推荐榜单:刮板薄膜蒸发器/薄膜蒸发器/刮板式蒸发器装备源头厂家精选
  • 单部电梯调度程序三次迭代设计与实践总结 - 23207231
  • hadoop与mysql的综合应用解决方案
  • 详细介绍:2. 容器常用操作
  • 一条SQL的完整执行过程:小明查询员工信息的完整冒险故事
  • 2025年便宜的化工品国际快递企业权威推荐榜单:药品国际快递/粉末国际快递/专线国际快递服务商精选
  • 杂题选做-6
  • 2025.11.22 考试总结
  • 新赛季临时脱产日记
  • 数据采集第3次作业
  • php openssl, RSA私钥有PKCS#1和PKCS#8,均包含有公钥
  • 2025 年 11 月中空吹塑机厂家推荐排行榜,吹塑机,挤出吹塑机,注射吹塑机,拉伸吹塑机,发泡吹塑机,工具箱吹塑机,瓶子吹塑机公司推荐
  • 2025.11.18 写题记录
  • 2025 最新支架厂家排行榜,出口级品质 + 定制服务 工程采购优选推荐指南热浸锌电缆/可调节角度隧道电缆沟/定制电缆沟/热镀锌电缆沟支架公司推荐
  • 渲染相关(Markdown、ByteMD、ReactMarkdown) - 实践
  • 2025年好吃不贵的餐厅服务权威推荐榜单:宝藏餐厅/好吃的餐厅/口碑好的餐厅服务精选
  • 2025年郑州婚姻心理咨询公司权威推荐榜单:心理健康咨询/家庭心理咨询/心理咨询源头公司精选
  • grub命令行启动linux
  • 2025 最新分频器厂家权威排行榜:EMF 三维电感技术加持,国际协会认证品质之选音响分频器/汽车音响分频器/喇叭分频器公司推荐
  • vue前端面试题——记录一次面试当中遇到的题(10) - 详解
  • HUST食堂解锁记录