FrameworkElement.RegisterName(String, Object) FrameworkElement.RegisterName(String, Object) FrameworkElement.RegisterName(String, Object) FrameworkElement.RegisterName(String, Object) Method

定義

NameScope の登録メソッドへのアクセスを簡略化するアクセサーを提供します。Provides an accessor that simplifies access to the NameScope registration method.

public:
 void RegisterName(System::String ^ name, System::Object ^ scopedElement);
public void RegisterName (string name, object scopedElement);
member this.RegisterName : string * obj -> unit
Public Sub RegisterName (name As String, scopedElement As Object)

パラメーター

name
String String String String

指定した名前オブジェクトの割り当てに使用する名前。Name to use for the specified name-object mapping.

scopedElement
Object Object Object Object

割り当て対象のオブジェクト。Object for the mapping.

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);
        
        }        
        
    }
    
      
}

Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes

Namespace Microsoft.Samples.Animation.AnimatingWithStoryboards




	Public Class ScopeExample
		Inherits Page

		Private myStoryboard As Storyboard
		Private myMainPanel As StackPanel
		Private button1, button2 As Button

		Public Sub New()

			Me.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"
			AddHandler button1.Click, AddressOf 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"
			AddHandler button2.Click, AddressOf button2Clicked
			myMainPanel.Children.Add(button2)


			' Create some animations and a storyboard.
			Dim button1WidthAnimation As New DoubleAnimation(300, 200, New Duration(TimeSpan.FromSeconds(5)))
			Storyboard.SetTargetName(button1WidthAnimation, button1.Name)
			Storyboard.SetTargetProperty(button1WidthAnimation, New PropertyPath(Button.WidthProperty))

			Dim button2WidthAnimation As New DoubleAnimation(300, 200, New Duration(TimeSpan.FromSeconds(5)))
			Storyboard.SetTargetName(button2WidthAnimation, button2.Name)
			Storyboard.SetTargetProperty(button2WidthAnimation, New PropertyPath(Button.WidthProperty))

			Dim heightAnimationWithoutTarget As 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)

			Me.Content = myMainPanel

		End Sub


		Private Sub button1Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)

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

		End Sub

		Private Sub button2Clicked(ByVal sender As Object, ByVal args As RoutedEventArgs)

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

		End Sub

	End Class


End Namespace

注釈

このメソッドを呼び出す便利なメソッドは、RegisterNameします。This method is a convenience method for calling RegisterName. 実装は、適切なが見つかるまで、連続する親要素は確認NameScope実装で、実装する要素の検索によって見つかったINameScopeします。The implementation will check successive parent elements until it finds the applicable NameScope implementation, which is found by finding an element that implements INameScope. 名前スコープの詳細については、次を参照してください。 WPF XAML 名前スコープします。For more information about namescopes, see WPF XAML Namescopes.

呼び出すRegisterName正しくコードで作成されたときに、アプリケーションのアニメーションのストーリー ボードをフックするために必要です。Calling RegisterName is necessary in order to correctly hook up animation storyboards for applications when created in code. これは、キーのいずれかのストーリー ボードのプロパティ、ためTargetName、ターゲット要素への参照を実行することではなく実行時の名の参照を使用します。This is because one of the key storyboard properties, TargetName, uses a run-time name lookup instead of being able to take a reference to a target element. これは、その要素が、コードから参照でアクセス可能な場合でも当てはまります。This is true even if that element is accessible by reference from the code. ストーリー ボード ターゲットの名前を登録する必要がある理由の詳細については、次を参照してください。ストーリー ボードの概要します。For more information on why you need to register names for storyboard targets, see Storyboards Overview.

適用対象

こちらもご覧ください