基本上,我如何用一个按钮激活一个简单的故事板动画,而不是让它自动播放?我在这里尝试普通按钮、切换按钮和切换开关,但没有一个有效。
我打算让门的图像在单击按钮时向右移动,并在再次单击时向左移动。单击时它应该无限期地重复该行为。
我的 MainPage.xaml 文件中有什么内容。
<Grid>
<Grid.Background>
<ImageBrush ImageSource="Assets/1.png"/>
</Grid.Background>
<Image Source="Assets/RightDoor.png" x:Name="rightdoor" HorizontalAlignment="Center" Height="1000" Margin="276,166,-2665,-398" VerticalAlignment="Top" Width="3755">
<Image.RenderTransform>
<!--<CompositeTransform x:Name="ImageTransform"/>-->
<TransformGroup>
<TranslateTransform x:Name="rdformTranslate" X ="1" Y="1" />
<ScaleTransform x:Name ="rdformScale" ScaleX=".25" ScaleY=".25" />
</TransformGroup>
</Image.RenderTransform>
</Image>
<ToggleButton x:Name="toggleButton" Content="ToggleButton" HorizontalAlignment="Left" Margin="493,572,0,0" VerticalAlignment="Top" Click="ToggleBtn"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="772,572,0,0" VerticalAlignment="Top" Click="BtnTest/>
<ToggleSwitch Header="ToggleSwitch" HorizontalAlignment="Left" Margin="202,548,0,0" VerticalAlignment="Top" Toggled="ToggleButton"/>
</Grid>
MainPage.xaml.cs 文件
private void ToggleButton(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Storyboard myStoryboard;
myStoryboard = (Storyboard)this.Resources["rdformTranslate"];
myStoryboard.Begin();
}
private void ToggleBtn(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Storyboard myStoryboard;
myStoryboard = (Storyboard)this.Resources["rdformTranslate"];
myStoryboard.Begin();
}
private void BtnTest(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Storyboard myStoryboard;
myStoryboard = (Storyboard)this.Resources["rdformTranslate"];
myStoryboard.Begin();
}
资源字典文件...
<Storyboard x:Name="rdformTranslate">
<DoubleAnimation Storyboard.TargetName="rdformTranslate"
Storyboard.TargetProperty="X"
From="0" To="500" Duration="0:0:1"
AutoReverse ="True" RepeatBehavior="Forever" />
</Storyboard>
该应用程序能够运行,但在单击任何按钮时,它会立即“崩溃”。我非常感谢解决问题的任何帮助。
编辑评论中添加的错误
在 mscorlib.dll 中发生了“System.Runtime.InteropServices.COMException”类型的异常,但未在用户代码中处理 WinRT 信息:E_NETWORK_ERROR 附加信息:未指定的错误 如果有此异常的处理程序,则程序可以安全地继续。