使用动画更改资源图像 [英] Change resources image with Animation
问题描述
我创建了一个项目,旨在通过动画更改背景图像.
我想通过一些动画来更改背景图像.
因此,在Grid(LayoutRoot)中,我为背景创建了静态资源(这使我可以使用不同的色彩组合以及图像资源),而对于图像资源则使用了ImageBrush.
我遇到类似无法解析TargetName Img"的错误.情节提要何时开始
XAML代码是这样的
I have created one project with an aim to change background image with animation.
I want to change background image with some animation.
So in Grid (LayoutRoot), I created a static resource for background (Which allows me to use different colour combimation as well as Image Resource) and for image resource I used ImageBrush.
I got some error like this " Cannot resolve TargetName Img." when storyboard begins
XAML Code is like this
<UserControl.Resources>
<ImageBrush x:Name="Img" x:Key="frmBackground" AlignmentX="Center" AlignmentY="Center" Stretch="UniformToFill" ImageSource="./Images/Background0.jpg"/>
<Storyboard x:Name="RotatingBkgrndImg">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Img">
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="0.99"/>
<EasingDoubleKeyFrame KeyTime="0:0:3" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:4" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="{StaticResource frmBackground}">
</Grid>
VB代码
VB Code
Public imgCounter As Integer = 0
Private Sub RotatingBkgrndImg_Completed(ByVal sender As Object, ByVal e As System.EventArgs) Handles RotatingBkgrndImg.Completed
If imgCounter = 9 Then
imgCounter = 0
Else
imgCounter = imgCounter + 1
End If
Img = Resources("frmBackground")
Dim loFile As String
loFile = "./Images/Background" & Trim(imgCounter.ToString) & ".Jpg"
Img.ImageSource = New Media.Imaging.BitmapImage(New Uri(loFile, UriKind.Relative))
RotatingBkgrndImg.Begin()
End Sub
Private Sub RectContent1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RectContent1.Loaded
RotatingBkgrndImg.Begin()
End Sub
请就上述问题指导我
谢谢
Kindly guide me on above issue
Thanks
推荐答案
"TargetName"在这里不起作用,因为"Img"不在Storyboard的范围内.此外,资源使用键"而不是名称".
我建议您在后面的代码中编写Storyboard动画并更改背景.
在Xaml以下可以解决您的问题.
"TargetName" would not work here as "Img" is not under Storyboard''s scope. Moreover Resources uses "Key" not the "Name".
I suggest you to write the Storyboard animation in the code behind and change your background.
Below Xaml should fix your problem.
<UserControl.Resources>
<ImageBrush x:Name="Img" x:Key="frmBackground" AlignmentX="Center" AlignmentY="Center" Stretch="UniformToFill" ImageSource="./Images/Background0.jpg"/>
<Storyboard x:Key="RotatingBkgrndImg" x:Name="RotatingBkgrndImg">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="backgroundBorder">
<EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="0.99"/>
<EasingDoubleKeyFrame KeyTime="0:0:3" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:4" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<Border x:Name="backgroundBorder" Background="{StaticResource frmBackground}"></Border>
</Grid>
我只是在网格内添加了边框,并将其名称用作TargetName.
如果有帮助,请将其标记为答案"
I just added a Border inside the Grid and used it''s name as TargetName.
Mark it as Answer if it is helpful
您好,Venkatesh,
谢谢您的答复
您能否提供一些适合我需要的示例代码
尼尔
Hi Venkatesh,
Thank you for your reply
Can you provide some sample code for study, as suitable to my needs
Anil
这篇关于使用动画更改资源图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!