棒棒牛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 885|回复: 5

[解决] 使用Storyboard实现动画浏览器可以窗口程序却无效问题 [复制链接]

V2

Rank: 2Rank: 2

游币
100
金币
121
经验
116
积分
148
帖子
43
发表于 2010-9-1 15:54:32 |显示全部楼层
下列程序,是在浏览器中的一个按钮实现旋转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();
             };
        
        }
        
    }
}

版主

WPF_1.5_ray

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

游币
259
金币
543
经验
1690
积分
2175
帖子
428

2011 年度优秀版主 2010 年度优秀版主

发表于 2010-9-1 17:19:59 |显示全部楼层
btn.RenderTransform = rotate;
            var ns = new NameScope();
            NameScope.SetNameScope(this, ns);            
            this.RegisterName("btn_Rotate", rotate);

            ......
            //Storyboard.SetTarget(animation, rotate);
            Storyboard.SetTargetName(animation, "btn_Rotate");

            ......
            storyboard.Begin(this);
1

查看全部评分

What is real?

V2

Rank: 2Rank: 2

游币
100
金币
121
经验
116
积分
148
帖子
43
发表于 2010-9-3 17:25:25 |显示全部楼层
真是万分感激了!
不过还是不了解,在page模式下,怎么就不要NameScope()?

版主

WPF_1.5_ray

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

游币
259
金币
543
经验
1690
积分
2175
帖子
428

2011 年度优秀版主 2010 年度优秀版主

发表于 2010-9-6 12:19:53 |显示全部楼层
Page模式下的Application声明周期控制跟Window模式下不一样。也许在里面有一些不同的处理
呵呵,我也没有细看,有时间拿Reflector看看
What is real?

V2

Rank: 2Rank: 2

游币
100
金币
121
经验
116
积分
148
帖子
43
发表于 2010-9-7 09:47:58 |显示全部楼层
回复 4# Ray


    再次感谢Ray!!!

V2

Rank: 2Rank: 2

游币
100
金币
121
经验
116
积分
148
帖子
43
发表于 2010-9-8 15:50:22 |显示全部楼层
多谢Ray!!!,修改后全文,真的是可以了呀。

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 动画测试
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial 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));

            var ns = new NameScope();
            NameScope.SetNameScope(this, ns);
            this.RegisterName("btn_Rotate", rotate);
            Storyboard.SetTargetName(anima, "btn_Rotate");
            Storyboard.SetTargetProperty(anima,
                new PropertyPath(RotateTransform.AngleProperty));
            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(anima);
            btn.Click += delegate
            {
                storyboard.Begin(this);
            };
        }
    }
}
您需要登录后才可以回帖 登录 | 注册

bbniu.com (湘ICP备06008909号)  

GMT+8, 2012-2-6 10:13

Copyright © 2009-2011 bbniu.com. All Rights Reserved.

回顶部