XAML动画:使用动画来显示或隐藏控件

3
我正在寻找一些关于如何使用XAML动画实现以下功能的指导/示例。我有一个简单的控件,用于显示某个图像。当用户单击该控件时,我需要显示另一个控件,该控件有用于操作图像的按钮。
我有用户控件1:
[图片]
我还有另一个用户控件2:
[图片]
使用动画,当用户在图像查看器上点击时,我需要将ImageControl显示在ImageViewer左上角。
请提供建议。
1个回答

5
你需要一个故事板,当用户与第一个UserControl交互时,会显示第二个UserControl。这可以通过多种方式实现,例如,我们可以使用不透明度或可见性来隐藏和显示第二个UserControl。
这是我的示例:
假设我有两个UserControl: 第一个UserControl(例如,ImageViewer)
<UserControl
    x:Class="XamlAnimation.MyUserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:XamlAnimation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <Grid Background="Cyan">
    </Grid>
</UserControl>

第二个用户控件(例如,一些按钮或控件)

<UserControl
    x:Class="XamlAnimation.MyUserControl2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:XamlAnimation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <StackPanel Orientation="Horizontal" Background="Black" 
                HorizontalAlignment="Left"
                VerticalAlignment="Top">
        <Button>Button 1</Button>
        <Button>Button 2</Button>
    </StackPanel>
</UserControl>

这里有一个包含两个用户控件的页面:

<Page
    x:Class="XamlAnimation.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:XamlAnimation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">   
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <local:MyUserControl1 Tapped="MyUserControl1_Tapped"/>
        <local:MyUserControl2 x:Name="myUserControl2" Opacity="0"/>
    </Grid>
</Page>

请注意,我将第二个UserControl的不透明度设置为零,这将在页面加载时隐藏第二个UserControl。
创建Storyboard最简单的方法是使用Blend。我们首先打开Blend中的页面并创建一个新的Storyboard资源。
一旦创建了Storyboard,Blend将进入录制模式。
然后选择第二个UserControl并选择您希望第二个UserControl出现的时间。
在那个时候,我们可以将第二个UserControl的不透明度值更改为100%,以便显示按钮。如果需要,您可以应用缓动函数使动画看起来更加平滑。
现在,关闭Blend并在Visual Studio中重新构建项目。您应该在页面上看到Storyboard资源。
<Page
    x:Class="XamlAnimation.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:XamlAnimation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.Resources>
        <Storyboard x:Name="ShowUserControl2">
            <DoubleAnimation Duration="0:0:2" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="myUserControl2" d:IsOptimized="True">
                <DoubleAnimation.EasingFunction>
                    <CircleEase EasingMode="EaseInOut"/>
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>
    </Page.Resources>
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <local:MyUserControl1 Tapped="MyUserControl1_Tapped"/>
        <local:MyUserControl2 x:Name="myUserControl2" Opacity="0"/>
    </Grid>
</Page>

最后,我们可以在代码中这样启动Storyboard:
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void MyUserControl1_Tapped(object sender, TappedRoutedEventArgs e)
    {
        ShowUserControl2.Begin();
    }
}

运行该项目,通过点击第一个UserControl,您应该能够看到动画效果。希望这可以帮助您!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接