共用方式為


HOW TO:定義名稱範圍

更新:2007 年 11 月

若要在程式碼中以 Storyboard 建立動畫,您必須建立 NameScope,並將目標物件的名稱註冊至擁有該名稱範圍的項目。下列範例建立了 myMainPanel 的 NameScope、將 button1 和 button2 兩個按鈕加入面格中,並註冊這兩個按鈕的名稱。另外還建立了幾個動畫和 Storyboard。這個腳本的 Begin 方法可用來啟動動畫。

因為 button1、button2 和 myMainPanel 全都共用相同的名稱範圍,所以任何一個都可以和 StoryboardBegin 方法一起使用,以啟動動畫。

範例

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Microsoft.Samples.Animation.AnimatingWithStoryboards
{




    public class ScopeExample : Page
    {

        private Storyboard myStoryboard;
        private StackPanel myMainPanel;
        private Button button1, button2;

        public ScopeExample()
        {

            this.Background = Brushes.White; 
            myMainPanel = new StackPanel();

            // Create a name scope for the stackpanel. 
            NameScope.SetNameScope(myMainPanel, new NameScope());

            myMainPanel.Background = Brushes.Orange;

            button1 = new Button();
            button1.Name = "Button1";

            // Register button1's name with myMainPanel.
            myMainPanel.RegisterName(button1.Name, button1);
            button1.Content = "Button 1";
            button1.Click += new RoutedEventHandler(button1Clicked);
            myMainPanel.Children.Add(button1);

            button2 = new Button();
            button2.Name = "Button2";

            // Register button2's name with myMainPanel.
            myMainPanel.RegisterName(button2.Name, button2);
            button2.Content = "Button 2";
            button2.Click += new RoutedEventHandler(button2Clicked);
            myMainPanel.Children.Add(button2);


            // Create some animations and a storyboard.
            DoubleAnimation button1WidthAnimation = 
                new DoubleAnimation(300, 200, new Duration(TimeSpan.FromSeconds(5)));
            Storyboard.SetTargetName(button1WidthAnimation, button1.Name);
            Storyboard.SetTargetProperty(button1WidthAnimation, new PropertyPath(Button.WidthProperty));

            DoubleAnimation button2WidthAnimation = 
                new DoubleAnimation(300, 200, new Duration(TimeSpan.FromSeconds(5)));
            Storyboard.SetTargetName(button2WidthAnimation, button2.Name);
            Storyboard.SetTargetProperty(button2WidthAnimation, new PropertyPath(Button.WidthProperty));

            DoubleAnimation heightAnimationWithoutTarget = 
                new DoubleAnimation(300, 200, new Duration(TimeSpan.FromSeconds(5)));
            Storyboard.SetTargetProperty(heightAnimationWithoutTarget, new PropertyPath(FrameworkElement.HeightProperty));                

            myStoryboard = new Storyboard();
            myStoryboard.Children.Add(button1WidthAnimation);
            myStoryboard.Children.Add(button2WidthAnimation);
            myStoryboard.Children.Add(heightAnimationWithoutTarget);

            this.Content = myMainPanel;

        }  


        private void button1Clicked(object sender, RoutedEventArgs args)
        {

            // Starts the animations. The animation without a specified 
            // target name, heightAnimationWithoutTarget, is applied to
            // myMainPanel.
            myStoryboard.Begin(myMainPanel);

        }

        private void button2Clicked(object sender, RoutedEventArgs args)
        {

            // Starts the animations. The animation without a specified 
            // target name, heightAnimationWithoutTarget, is applied to
            // button2.
            myStoryboard.Begin(button2);

        }        

    }


}

請參閱

工作

HOW TO:使用腳本建立屬性的動畫

概念

動畫概觀