UWP 动画改变控件大小(高度)

有这样一个需求:

  • 鼠标移动到(悬停在)控件上(PointerEntered),控件大小(高度)发生变化,以显示更多内容;
  • 鼠标移出控件(PointerExited),控件大小恢复原状。

本文通过 UWP 动画,用两种方法实现这个效果,用于改变周贡献榜和粉丝榜的 Grid 的高度。

方法一:XAML 实现动画

XAML:

<UserControl.Resources>
    <!--周贡、粉丝榜下拉恢复动画-->
    <Storyboard x:Name="SeeMoreAnimation" Storyboard.TargetName="WeekFansGrid">
        <DoubleAnimation Duration="0:0:0.2" EnableDependentAnimation="True" Storyboard.TargetProperty="Height" From="140" To="400"/>
    </Storyboard>
    <Storyboard x:Name="RestoreAnimation" Storyboard.TargetName="WeekFansGrid">
        <DoubleAnimation  Duration="0:0:0.2" EnableDependentAnimation="True" Storyboard.TargetProperty="Height" From="400" To="140"/>
    </Storyboard>
</UserControl.Resources>

<Grid x:Name="WeekFansGrid" Background="White" VerticalAlignment="Top" Height="140"
              PointerEntered="Grid_PointerEntered" PointerExited="Grid_PointerExited">
    <!--Grid 里面的一些内容-->
</Grid>

C#:

// 鼠标悬停周贡、粉丝榜的 Grid
private void Grid_PointerEntered(object sender, PointerRoutedEventArgs e)
{
    try
    {
        SeeMoreAnimation.Begin();
    }
    catch (Exception e1)
    {
        System.Diagnostics.Debug.WriteLine("Grid_PointerEntered " + e1.Message.ToString());
    }
}

// 鼠标离开周贡、粉丝榜的 Grid
private void Grid_PointerExited(object sender, PointerRoutedEventArgs e)
{
    try
    {
        RestoreAnimation.Begin();
    }
    catch (Exception e1)
    {
        System.Diagnostics.Debug.WriteLine("Grid_PointerExited " + e1.Message.ToString());
    }
}

方法二:后台实现动画

XAML:

<Grid x:Name="WeekFansGrid" Background="White" VerticalAlignment="Top" Height="140"
              PointerEntered="Grid_PointerEntered" PointerExited="Grid_PointerExited">
    <!--Grid 里面的一些内容-->     
</Grid>

C#:

// 鼠标悬停周贡、粉丝榜的 Grid
private void Grid_PointerEntered(object sender, PointerRoutedEventArgs e)
{
    try
    {
        Grid grid = sender as Grid;
        if (grid != null)
        {
            DoubleAnimation SeeMoreAnimation = new DoubleAnimation();
            if (SeeMoreAnimation != null)
            {
                // 高度从 140 变化到 400
                SeeMoreAnimation.From = 140;
                SeeMoreAnimation.To = 400;
                // 用时 200 毫秒
                SeeMoreAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(200));
                SeeMoreAnimation.EnableDependentAnimation = true;

                // 目标 Grid 的 Height
                Storyboard.SetTarget(SeeMoreAnimation, grid);
                Storyboard.SetTargetProperty(SeeMoreAnimation, "Height");

                Storyboard storyboard = new Storyboard();
                if (storyboard != null)
                {
                    storyboard.Children.Add(SeeMoreAnimation);
                    // 执行动画
                    storyboard.Begin();
                }
            }
        }
    }
    catch (Exception e1)
    {
        System.Diagnostics.Debug.WriteLine("Grid_PointerEntered " + e1.Message.ToString());
    }
}

// 鼠标离开周贡、粉丝榜的 Grid
private void Grid_PointerExited(object sender, PointerRoutedEventArgs e)
{
    try
    {
        Grid grid = sender as Grid;
        if (grid != null)
        {
            DoubleAnimation SeeMoreAnimation = new DoubleAnimation();
            if (SeeMoreAnimation != null)
            {
                // 高度从 400 变化到 140
                SeeMoreAnimation.From = 400;
                SeeMoreAnimation.To = 140;
                // 用时 200 毫秒
                SeeMoreAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(200));
                SeeMoreAnimation.EnableDependentAnimation = true;

                // 目标 Grid 的 Height
                Storyboard.SetTarget(SeeMoreAnimation, grid);
                Storyboard.SetTargetProperty(SeeMoreAnimation, "Height");

                Storyboard storyboard = new Storyboard();
                if (storyboard != null)
                {
                    storyboard.Children.Add(SeeMoreAnimation);
                    // 执行动画
                    storyboard.Begin();
                }
            }
        }
    }
    catch (Exception e1)
    {
        System.Diagnostics.Debug.WriteLine("Grid_PointerExited " + e1.Message.ToString());
    }
}

实现效果

GridSeeMoreAnimation

WinUI 控件UWP 控件、WPF 控件和 Silverlight 控件在语法和结构上有所不同,因此可以通过检查 XAML 代码的命名空间来区分它们。以下是一些常见的命名空间和控件: - WinUI 控件:命名空间为 `http://schemas.microsoft.com/winui/2021/xaml/behaviors` 或 `http://schemas.microsoft.com/winui/2021/xaml/presentation`,控件名称以 `Microsoft.UI` 开头。 - UWP 控件:命名空间为 `http://schemas.microsoft.com/winfx/2006/xaml/presentation` 或 `http://schemas.microsoft.com/winfx/2008/xaml/presentation`,控件名称以 `Windows.UI` 开头。 - WPF 控件:命名空间为 `http://schemas.microsoft.com/winfx/2006/xaml/presentation` 或 `http://schemas.microsoft.com/netfx/2007/xaml/presentation`,控件名称以 `System.Windows` 或 `Microsoft.Windows` 开头。 - Silverlight 控件:命名空间为 `http://schemas.microsoft.com/winfx/2006/xaml/presentation` 或 `http://schemas.microsoft.com/client/2007`,控件名称以 `System.Windows.Controls` 或 `Microsoft.Windows.Controls` 开头。 可以通过读取 XAML 文件中的命名空间来确定使用的控件类型。例如,以下代码片段演示了如何读取 XAML 文件中的命名空间: ```csharp using System.Xml.Linq; // Load XAML file into an XDocument XDocument xdoc = XDocument.Load("MyXamlFile.xaml"); // Get the root element of the XAML file XElement root = xdoc.Root; // Get the default namespace of the XAML file XNamespace ns = root.GetDefaultNamespace(); // Check the namespace to determine the type of controls used in the XAML file if (ns.NamespaceName.StartsWith("http://schemas.microsoft.com/winui")) { // WinUI controls } else if (ns.NamespaceName.StartsWith("http://schemas.microsoft.com/winfx")) { // UWP or WPF controls } else if (ns.NamespaceName.StartsWith("http://schemas.microsoft.com/client")) { // Silverlight controls } else { // Unknown namespace } ``` 请注意,这只是一种简单的方法来区分不同类型的控件,实际上还需要考虑一些其他因素,例如控件的属性和行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值