WPF中如何用Storyboard

glacier000 2011-07-06 04:50:55
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="-1"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="-1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>


上面是在Blend中做好的一段翻转动画代码,想在后台动态定义,该如何写呢?
似乎和SL不太一样,在SL里的方式,拿到WPF中就会报错

Storyboard keyFrameboard = new Storyboard();

DoubleAnimationUsingKeyFrames dakeyframe = new DoubleAnimationUsingKeyFrames();

Storyboard.SetTarget(dakeyframe, icCurrent);
Storyboard.SetTargetProperty(dakeyframe, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));
dakeyframe.BeginTime = new TimeSpan(0, 0, 0);

EasingDoubleKeyFrame edKeyFrame = new EasingDoubleKeyFrame();
edKeyFrame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1));
edKeyFrame.Value = -1;
dakeyframe.KeyFrames.Add(edKeyFrame);

keyFrameboard.Children.Add(dakeyframe);

keyFrameboard.Begin();


上面是我的写法,但报如下错误:

Cannot resolve all property references in the property path '(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)'. Verify that applicable objects support the properties.

请高手指教!!!
...全文
2335 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
aggier 2013-04-20
  • 打赏
  • 举报
回复
哦,知道了,是我控件类型没有转换。呵呵……
aggier 2013-04-19
  • 打赏
  • 举报
回复
额,刚刚我也遇到这个问题,我是自定义一个behavior 然后引用这个behavior,结果就报这个错。 Cannot resolve all property references in the property path '(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)'. Verify that applicable objects support the properties. 我直接在xaml中添加storyboard的没有问题的?看了下面几位高手说自定义控件有问题?但我不知道怎么有问题,还请大家多多帮忙,谢了。下面是我的代码 //清除故事版和资源 textBlock = this.AssociatedObject as TextBlock; DoubleAnimationUsingKeyFrames daX = new DoubleAnimationUsingKeyFrames(); DoubleAnimationUsingKeyFrames daY = new DoubleAnimationUsingKeyFrames(); sboard.Children.Clear(); frameX.Value = -4; frameX.KeyTime = TimeSpan.FromMilliseconds(200); daX.KeyFrames.Add(frameX); sboard.Children.Add(daX); frameY.Value = -10.6; frameY.KeyTime = TimeSpan.FromMilliseconds(200); daY.KeyFrames.Add(frameY); daX.Duration = new Duration(TimeSpan.FromMilliseconds(200)); daY.Duration = new Duration(TimeSpan.FromMilliseconds(200)); Storyboard.SetTarget(daX, textBlock); Storyboard.SetTarget(daY, textBlock); Storyboard.SetTargetProperty(daX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)")); Storyboard.SetTargetProperty(daY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)")); sboard.Children.Add(daY); sboard.Duration = daX.Duration; textBlock.Resources.Add("Storyboard", sboard); xaml中: <TextBlock Text="sdfdfdsfdsfdsfsdf"> <i:Interaction.Behaviors> <me:SelectSongBehvior /> </i:Interaction.Behaviors> </TextBlock>
Mr_Leaco 2012-10-22
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]

另外啰嗦一句,你的自定义控件写法有问题,不过这个需要慢慢积累的,慢慢来吧,有时间看看wpf和示例的Toolkit中的控件源码,很有帮助的
[/Quote]

To:duanzilin

高手大哥,我的WPF写的动画只要在界面更新的时候都会卡顿一下,您有什么解决办法嘛,十分感谢!
glacier000 2011-07-07
  • 打赏
  • 举报
回复
Thank you Thank you, 能翻了,哈哈,非常感谢,小弟初学WPF,见笑了 :)
沝林 2011-07-07
  • 打赏
  • 举报
回复
另外啰嗦一句,你的自定义控件写法有问题,不过这个需要慢慢积累的,慢慢来吧,有时间看看wpf和示例的Toolkit中的控件源码,很有帮助的
沝林 2011-07-07
  • 打赏
  • 举报
回复
看了你的代码,还是因为没有定义

要让自定义控件进行旋转变换,需要对自定义控件的转换进行定义,你在自定义控件里面定义的转变,在mainwindow中找不到的

正确方法如下,有两种:

一、xaml方式

<Canvas x:Name="canvasBottom" Width="500" Height="500" Background="Gray" Grid.Row="1">
<my:ImageContainer x:Name="ic" Width="200" Height="200" >
<my:ImageContainer.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</my:ImageContainer.RenderTransform>
</my:ImageContainer>
</Canvas>


二、代码方式


ImageContainer ic = new ImageContainer();
ic.Width = 150;
ic.Height = 150;
canvasBottom.Children.Add(ic);

icCurrent = ic;

ScaleTransform myScaleTransform = new ScaleTransform();
myScaleTransform.ScaleY = 1;
myScaleTransform.ScaleX = 1;

RotateTransform myRotateTransform = new RotateTransform();
myRotateTransform.Angle = 0;

TranslateTransform myTranslate = new TranslateTransform();

SkewTransform mySkew = new SkewTransform();

// Create a TransformGroup to contain the transforms
// and add the transforms to it.
TransformGroup myTransformGroup = new TransformGroup();
myTransformGroup.Children.Add(myScaleTransform);
myTransformGroup.Children.Add(myRotateTransform);
myTransformGroup.Children.Add(myTranslate);
myTransformGroup.Children.Add(mySkew);

icCurrent.RenderTransform = myTransformGroup;
glacier000 2011-07-07
  • 打赏
  • 举报
回复
请查收 :)
沝林 2011-07-07
  • 打赏
  • 举报
回复
zilin_duan@hotmail.com
glacier000 2011-07-07
  • 打赏
  • 举报
回复
To:duanzilin
高手有时间吗,留个邮箱,我把例子发给你,帮看看,是哪里有问题了
非常感谢
沝林 2011-07-07
  • 打赏
  • 举报
回复
控件也可以翻转的,是不是你什么地方写的有问题
glacier000 2011-07-07
  • 打赏
  • 举报
回复
我是用grid做了一个自定义控件,然后旋转这个自定义控件,就会报错,会不会和自定义控件有关系呢
沝林 2011-07-07
  • 打赏
  • 举报
回复
这个是我在wpf中测试的全部源码,你可以测试一下:


<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="icCurrent" Margin="0,200,0,0" Background="Blue">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
</TransformGroup>
</Grid.RenderTransform>
</Grid>

</Window>


cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Media.Animation;

namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

Storyboard keyFrameboard = new Storyboard();

DoubleAnimationUsingKeyFrames dakeyframe = new DoubleAnimationUsingKeyFrames();

Storyboard.SetTarget(dakeyframe, icCurrent);
Storyboard.SetTargetProperty(dakeyframe, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));
dakeyframe.BeginTime = new TimeSpan(0, 0, 0);

EasingDoubleKeyFrame edKeyFrame = new EasingDoubleKeyFrame();
edKeyFrame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1));
edKeyFrame.Value = -1;
dakeyframe.KeyFrames.Add(edKeyFrame);

keyFrameboard.Children.Add(dakeyframe);

keyFrameboard.Begin();



}
}
}

glacier000 2011-07-07
  • 打赏
  • 举报
回复
WPF中?
沝林 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 glacier000 的回复:]
To:duanzilin

谢谢,但是我已经在Grid中加了这些内容,还是报错
[/Quote]

那就奇怪了,我把你的cs代码部分整个复制下来的,没有修改任何东西,运行没错的
glacier000 2011-07-07
  • 打赏
  • 举报
回复
To:duanzilin

谢谢,但是我已经在Grid中加了这些内容,还是报错
沝林 2011-07-06
  • 打赏
  • 举报
回复
试了一下你的代码,Cs代码是完全没有问题的,不过需要先在xaml中添加对象转换定义,否则会无法解析属性路径:


<Grid x:Name="icCurrent" Margin="0,200,0,0" Background="AliceBlue">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
</TransformGroup>
</Grid.RenderTransform>
</Grid>

Jane_sl 2011-07-06
  • 打赏
  • 举报
回复
路过,帮顶下

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧