我正在制作一个UWP项目,当我在Windows上浏览我的设置应用程序时,我意识到每当我按下一个按钮时,就会有一条蓝线移动. 我想为我的项目用UWP重新制作它,因为它看起来很好. 但每当动画播放时,它都会在实际按钮下方移动几个100个EPX.
为什么?
XAML脚本:
<Page
x:Class="Library.Views.MainMenuView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Library.Helpers"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationBackgroundBrush}">
<Grid>
<Border HorizontalAlignment="Left" MaxWidth="400" MinWidth="200" Width="Auto" Height="Auto" Background="{ThemeResource ControlBackgroundBrush}" BorderBrush="{ThemeResource ControlHoveredBorderBrush}" BorderThickness="1" CornerRadius="0,8,8,0" >
<StackPanel x:Name="Buttons">
<Button x:Name="Home" Style="{StaticResource UIButtonStyle}" Width="300" Height="40" FontSize="20" HorizontalAlignment="Center" Margin="5,8,12,8" Click="Button_Click">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Image Source="/Assets/HomeNoBGRes.png" Width="35" Height="30" Margin ="0,0,10,0" Stretch="Fill"/>
<TextBlock Text="Home" FontSize="20" Width="240"/>
</StackPanel>
</Button>
<Button x:Name="Projects" Style="{StaticResource UIButtonStyle}" Width="300" Height="40" FontSize="20" HorizontalAlignment="Center" Margin="5,8,12,8" Click="Button_Click">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Image Source="/Assets/ProjectsNoBGRes.png" Width="30" Height="30" Margin ="0,0,10,0" Stretch="Fill"/>
<TextBlock Text="My Projects" FontSize="20" Width="240"/>
</StackPanel>
</Button>
<Border Background="{ThemeResource ControlHoveredBackgroundBrush}" Width="270" Height="5" CornerRadius="3" HorizontalAlignment="Center"/>
<Button x:Name="Documents" Style="{StaticResource UIButtonStyle}" Width="300" Height="40" FontSize="20" HorizontalAlignment="Center" Margin="5,8,12,8" Click="Button_Click">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Image Source="/Assets/DocsNoBGRes.png" Width="30" Height="30" Margin ="0,0,10,0" Stretch="Fill"/>
<TextBlock Text="Documents" FontSize="20" Width="240"/>
</StackPanel>
</Button>
<Button x:Name="Paper" Style="{StaticResource UIButtonStyle}" Width="300" Height="40" FontSize="20" HorizontalAlignment="Center" Margin="5,8,12,8" Click="Button_Click">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Image Source="/Assets/PaperNoBGRes.png" Width="30" Height="30" Margin ="0,0,10,0" Stretch="Fill"/>
<TextBlock Text="Paper" FontSize="20" Width="240"/>
</StackPanel>
</Button>
</StackPanel>
</Border>
<Border x:Name="SelectionIndicator" Visibility="Collapsed" Background="{ThemeResource SystemAccentColor}" Height="30" Width="5" CornerRadius="3" HorizontalAlignment="Left" Margin="5,0,0,0"/>
</Grid>
</Page>
和C#动画函数:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (SelectionIndicator.Visibility == Visibility.Collapsed) SelectionIndicator.Visibility = Visibility.Visible;
if (sender is Button clickedButton)
{
double indicatorLeft = clickedButton.TransformToVisual(Buttons).TransformPoint(new Point(0, 0)).Y;
if (SelectionIndicator.RenderTransform is null || !(SelectionIndicator.RenderTransform is CompositeTransform)) SelectionIndicator.RenderTransform = new CompositeTransform();
CompositeTransform transform = SelectionIndicator.RenderTransform as CompositeTransform;
Storyboard storyboard = new Storyboard();
DoubleAnimation moveAnimation = new DoubleAnimation
{
To = indicatorLeft,
Duration = TimeSpan.FromSeconds(0.5),
EnableDependentAnimation = true,
EasingFunction = new CircleEase { EasingMode= EasingMode.EaseInOut },
};
Storyboard.SetTarget(moveAnimation, transform);
Storyboard.SetTargetProperty(moveAnimation, "TranslateY");
storyboard.Children.Add(moveAnimation);
storyboard.Begin();
}
}
这是一张展示我的问题的图片:
我试着在边框上添加一个网格,因为这可能是因为它们不在同一个容器中,但不是.
提前谢谢!
(致版主:不要抹杀我对预付款的感谢,这些是为了感谢那些真正帮助我的人)