- 经验
- 116
- 金币
- 121
- 游币
- 100
- 最后登录
- 2011-11-18
- 注册时间
- 2010-8-14
- 帖子
- 43
- 积分
- 148
- 阅读权限
- 20
- UID
- 3430
 
- 游币
- 100
- 金币
- 121
- 经验
- 116
- 积分
- 148
- 帖子
- 43
|
下列程序,是在浏览器中的一个按钮实现旋转1周的动画,可以:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace NoXamlApp
{
public class App : Application
{
public App()
{
ContentControl root = new ContentControl();
RootVisual = root;
Button btn = new Button();
btn.Content = "Click me!";
btn.FontSize = 96;
btn.Margin = new Thickness(192);
btn.RenderTransformOrigin = new Point(0.5, 0.5);
root.Content = btn;
RotateTransform rotate = new RotateTransform();
btn.RenderTransform = rotate;
DoubleAnimation anima = new DoubleAnimation();
anima.From = 0;
anima.To = 360;
anima.Duration =
new Duration(TimeSpan.FromSeconds(0.25));
Storyboard.SetTarget(anima, rotate);
Storyboard.SetTargetProperty(anima,
new PropertyPath(RotateTransform.AngleProperty));
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(anima);
btn.Click += delegate
{
storyboard.Begin();
};
}
}
}
但是,如下改为Window按钮确不可旋转了,不知为什么?
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace WpfApplication1
{
public class iProgram : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new iProgram());
}
public iProgram()
{
Title = "Grid布局";
Button btn = new Button();
btn.Content = "Click me!";
btn.FontSize = 96;
btn.Margin = new Thickness(192);
btn.RenderTransformOrigin = new Point(0.5, 0.5);
this.Content = btn;
RotateTransform rotate = new RotateTransform();
btn.RenderTransform = rotate;
DoubleAnimation anima = new DoubleAnimation();
anima.From = 0;
anima.To = 360;
anima.Duration =
new Duration(TimeSpan.FromSeconds(0.25));
Storyboard.SetTarget(anima, rotate);
Storyboard.SetTargetProperty(anima,
new PropertyPath(RotateTransform.AngleProperty));
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(anima);
btn.Click += delegate
{
storyboard.Begin();
};
}
}
} |
|