1

我创建了一个 Silverlight 应用程序。我想翻转图像,所以我从后面的代码创建了一个故事板。但它抛出错误"Cannot resolve target name 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();

我哪里错了???

4

1 回答 1

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);
于 2012-09-06T06:34:13.143 回答