我正在寻找一些关于如何使用XAMl动画启用以下内容的指南/示例.我有一个简单的控件,显示一些图像.单击控件时,我需要显示另一个控件,该控件具有用于操作图像的按钮.
我有用户控件1:
我有另一个用户控件2:
使用动画我需要在用户点击图像查看器时在左侧角落的ImageViewer上显示ImageControl.
请提供意见
您需要的是一个故事板,当用户与第一个UserControl进行交互时,它将显示UserControl 2.这可以通过多种方式完成,例如,我们可以使用不透明度或可见性隐藏和显示第二个UserControl.
这是我的样本:
假设我有两个UserControl:
1st 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>
Run Code Online (Sandbox Code Playgroud)
第二个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>
Run Code Online (Sandbox Code Playgroud)
这是包含UserControls的Page:
<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>
Run Code Online (Sandbox Code Playgroud)
请注意我将第二个UserControl的不透明度设置为零,这将在加载页面时隐藏第二个UserControl.
创建故事板的最简单方法是使用Blend.我们首先使用Blend打开页面并创建一个新的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>
Run Code Online (Sandbox Code Playgroud)
最后,我们可以在代码隐藏中启动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();
}
}
Run Code Online (Sandbox Code Playgroud)
运行该项目,您应该能够通过点击第一个UserControl来查看动画.希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
2437 次 |
| 最近记录: |