记录一次Avalonia控件封装

public partial class DataLoadingView : UserControl
{
    public static readonly StyledProperty<string> NotifyTextProperty =
        AvaloniaProperty.Register<DataLoadingView, string>("NotifyText", "加载中...");


    public static readonly StyledProperty<int> IconWidthProperty =
        AvaloniaProperty.Register<DataLoadingView, int>("IconWidth", 50);

    public static readonly StyledProperty<int> IconHeightProperty =
        AvaloniaProperty.Register<DataLoadingView, int>("IconHeight", 50);


    public static readonly StyledProperty<int> CircleHeightProperty =
        AvaloniaProperty.Register<DataLoadingView, int>("CircleHeight", 70);

    public static readonly StyledProperty<int> CircleWidthProperty =
        AvaloniaProperty.Register<DataLoadingView, int>("CircleWidth", 70);

    public string NotifyText
    {
        get => GetValue(NotifyTextProperty);
        set => SetValue(NotifyTextProperty, value);
    }
	
	 public int IconWidth
    {
        get => GetValue(IconWidthProperty);
        set => SetValue(IconWidthProperty, value);
    }

    public int IconHeight
    {
        get => GetValue(IconHeightProperty);
        set => SetValue(IconHeightProperty, value);
    }

    public int CircleHeight
    {
        get => GetValue(CircleHeightProperty);
        set => SetValue(CircleHeightProperty, value);
    }

    public int CircleWidth
    {
        get => GetValue(CircleWidthProperty);
        set => SetValue(CircleWidthProperty, value);
    }

    public DataLoadingView()
    {
        InitializeComponent();
    }
}

<UserControl 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="clr-namespace:AvaloniaApplication1.Views"
             mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="200"
             x:Class="AvaloniaApplication1.Views.DataLoadingView">
    <UserControl.Styles>
        <Style Selector="Image.bg">

            <Setter Property="Opacity" Value="1"></Setter>
            <Style.Animations>
                <Animation Duration="0:0:2" IterationCount="INFINITE">
                    <KeyFrame Cue="0%">
                        <Setter Property="RotateTransform.Angle" Value="0" />
                    </KeyFrame>
                    <KeyFrame Cue="100%">
                        <Setter Property="RotateTransform.Angle" Value="360" />
                    </KeyFrame>
                </Animation>
            </Style.Animations>
        </Style>
    </UserControl.Styles>
    <Grid RowDefinitions="Auto,12,Auto,*">
        <Image Grid.Row="0" Source="../Assets/bg.png" 
               Width="{Binding  $parent[local:DataLoadingView].IconWidth}"
               Height="{Binding  $parent[local:DataLoadingView].IconHeight}">

        </Image>
        <Image Grid.Row="0" Classes="bg" Source="../Assets/circle_bg2.png"
               Width="{Binding  $parent[local:DataLoadingView].CircleWidth}"
               Height="{Binding  $parent[local:DataLoadingView].CircleHeight}">

        </Image>
        <TextBlock TextAlignment="Center" Grid.Row="2" VerticalAlignment="Center"
                   Text="{Binding  $parent[local:DataLoadingView].NotifyText}"
                   Foreground="#eb5a10" FontSize="14" FontFamily="Source Han Sans CN">
        </TextBlock>
    </Grid>
</UserControl>
posted @ 2025-11-10 15:56  Timskt  阅读(5)  评论(0)    收藏  举报