CachingTest.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="600" Width="800"
        x:Class="AvaloniaUI.CachingTest"
         x:Name="window"
        Title="CachingTest">
    <Window.Resources>
        <ArithmeticConverter x:Key="converter" />
    </Window.Resources>
    
    <Grid Margin="5" RowDefinitions="*,auto">
        <Canvas Name="canvas">
            <Path Name="pathBackground" Stroke="DarkRed" StrokeThickness="1" ></Path>
            <Rectangle Name="rect" Canvas.Left="10" Canvas.Top="100" Fill="Blue" Width="75" Height="75">
                <Rectangle.Styles>
                    <Style Selector="Rectangle">
                        <Style.Animations>
                            <Animation Duration="0:0:10" IterationCount="Infinite">
                                <KeyFrame Cue="0%">
                                    <Setter Property="Canvas.Left" Value="10"/>
                                </KeyFrame>

                                <KeyFrame Cue="100%">
                                    <Setter Property="Canvas.Left"
                                            Value="{Binding #window.Width, Converter={StaticResource converter}, ConverterParameter=-100}" />
                                </KeyFrame>
                            </Animation>
                        </Style.Animations>
                    </Style>
                </Rectangle.Styles>
            </Rectangle>
        </Canvas>

        <CheckBox Grid.Row="2" Name="chkCache" Content="Enable Caching"
                  IsChecked="False" Click="chkCache_Click"></CheckBox>
    </Grid>
</Window>

CachingTest.axaml.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using System;
using System.Text;

namespace AvaloniaUI;

public partial class CachingTest : Window
{
    public CachingTest()
    {
        InitializeComponent();
        int maxHeight = (int)this.Height;
        int maxWidth = (int)this.Width;

        var rand = new Random();
        var sb = new StringBuilder();

        // PathGeometry 用字符串构造
        sb.Append("M 0,0 ");
        for (int i = 0; i < 500; i++)
        {
            sb.Append($"L {rand.Next(0, maxWidth)},{rand.Next(0, maxHeight)} ");
        }

        var geometry = Geometry.Parse(sb.ToString());
        pathBackground.Data = geometry;

        chkCache.Click += chkCache_Click;
    }

    private void chkCache_Click(object? sender, RoutedEventArgs e)
    {
        if (chkCache.IsChecked == true)
        {
            var rtb = new RenderTargetBitmap(new PixelSize((int)Bounds.Width, (int)Bounds.Height));
            rtb.Render(pathBackground);
            pathBackground.Fill = new ImageBrush(rtb);
        }
        else
        {
            pathBackground.Fill = null; // 移除缓存,恢复原始绘制
        }
    }
}

运行效果

 

posted on 2025-09-28 15:18  dalgleish  阅读(13)  评论(0)    收藏  举报