我创建了一个silverlight应用程序。我想翻转图像,所以我从后面的代码创建了一个故事板。但它引发了错误"无法解析目标名称Imagename" 。
Storyboard sbFlip = new Storyboard();
sbFlip.Duration = new Duration(TimeSpan.FromSeconds(3));
DoubleAnimationUsingKeyFrames FlipFront = new DoubleAnimationUsingKeyFrames();
DoubleAnimationUsingKeyFrames FlipBack = new DoubleAnimationUsingKeyFrames();
Storyboard.SetTargetName(FlipFront, strFrontSelectedValue);
Storyboard.SetTargetName(FlipBack, strBackSelectedValue);
Storyboard.SetTargetProperty(FlipFront, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleX)"));
Storyboard.SetTargetProperty(FlipBack, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleX)"));
SplineDoubleKeyFrame sFlipFront = new SplineDoubleKeyFrame();
SplineDoubleKeyFrame sFlipBack = new SplineDoubleKeyFrame();
sFlipFront.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(500));
sFlipFront.Value = 0;
sFlipBack.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(500));
sFlipBack.Value = 1;
FlipFront.KeyFrames.Add(sFlipFront);
FlipBack.KeyFrames.Add(sFlipBack);
sbFlip.Children.Add(FlipFront);
sbFlip.Children.Add(FlipBack);
sbFlip.AutoReverse = true;
sbFlip.Completed += new EventHandler(this.sbFlip_Completed);
sbFlip.Begin();
我哪里出错???
答案 0 :(得分:1)
哇得到了答案。我必须将字符串转换为图像并传递给函数,然后添加到目标,现在发生图像的翻转。
.cs页面:
Storyboard sbFlip = new Storyboard();
sbFlip.Duration = new Duration(TimeSpan.FromSeconds(3));
DoubleAnimationUsingKeyFrames FlipFront = new DoubleAnimationUsingKeyFrames();
DoubleAnimationUsingKeyFrames FlipBack = new DoubleAnimationUsingKeyFrames();
Storyboard.SetTargetName(FlipFront, strFrontSelectedValue);
Storyboard.SetTargetName(FlipBack, strBackSelectedValue);
Storyboard.SetTarget(FlipFront, imgFront);
Storyboard.SetTarget(FlipBack, imgBack);
Storyboard.SetTargetProperty(FlipFront, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleX)"));
Storyboard.SetTargetProperty(FlipBack, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleX)"));
SplineDoubleKeyFrame sFlipFront = new SplineDoubleKeyFrame();
SplineDoubleKeyFrame sFlipBack = new SplineDoubleKeyFrame();
sFlipFront.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(500));
sFlipFront.Value = 0;
sFlipBack.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(500));
sFlipBack.Value = 1;
FlipFront.KeyFrames.Add(sFlipFront);
FlipBack.KeyFrames.Add(sFlipBack);
sbFlip.Children.Add(FlipFront);
sbFlip.Children.Add(FlipBack);
sbFlip.AutoReverse = true;
sbFlip.Completed += new EventHandler(this.sbFlip_Completed);
sbFlip.Begin();
//传递字符串并查找为图像
Image imgBack = FindControl<Image>((UIElement)Layout, typeof(Image), strSelectedimg);
//查找图像的功能
public T FindControl<T>(UIElement parent, Type targetType, string ControlName) where T : FrameworkElement
{
if (parent == null) return null;
if (parent.GetType() == targetType && ((T)parent).Name == ControlName)
{
return (T)parent;
}
T result = null;
int count = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < count; i++)
{
UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
if (FindControl<T>(child, targetType, ControlName) != null)
{
result = FindControl<T>(child, targetType, ControlName);
break;
}
}
return result;
}
//在函数中添加这两行
Storyboard.SetTarget(FlipFront, imgFront);
Storyboard.SetTarget(FlipBack, imgBack);