FrameworkElement.RegisterName(String, Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供可簡化對 NameScope 註冊方法之存取的存取子 (Accessor)。
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
要用於指定之「名稱-物件」對應的名稱。
- scopedElement
- Object
對應的物件。
範例
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.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 的便利方法。 實作會檢查後續的父元素,直到找到適用的 NameScope 實作為止,方法是尋找實作 的 INameScope 元素。 如需名稱範圍的詳細資訊,請參閱 WPF XAML 名稱範圍。
RegisterName呼叫 是必要的,才能在程式碼中建立時正確連結應用程式的動畫腳本。 這是因為其中一個主要分鏡腳本屬性會 TargetName 使用執行時間名稱查閱,而不是能夠參考目標元素。 即使可從程式碼參考存取該元素,也是如此。 如需為何需要註冊分鏡腳本目標名稱的詳細資訊,請參閱 分鏡腳本概觀。