HOW TO:處理 Loaded 事件
本範例示範如何處理 FrameworkElement.Loaded 事件,以及處理該事件的適當情況。 當頁面載入時,處理常式會建立 Button。
範例
下列範例使用 Extensible Application Markup Language (XAML) 以及程式碼後置 (Code-Behind) 的檔案。
<StackPanel
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SDKSample.FELoaded"
Loaded="OnLoad"
Name="root"
>
</StackPanel>
Private Sub OnLoad(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim b1 As Button = New Button()
b1.Content = "New Button"
root.Children.Add(b1)
b1.Height = 25
b1.Width = 200
b1.HorizontalAlignment = HorizontalAlignment.Left
End Sub
void OnLoad(object sender, RoutedEventArgs e)
{
Button b1 = new Button();
b1.Content = "New Button";
root.Children.Add(b1);
b1.Height = 25;
b1.Width = 200;
b1.HorizontalAlignment = HorizontalAlignment.Left;
}