Just a note to myself...
Lambda 表達式
//Lambda表達式
this.myButton.Click += ( sender, e) =>
{
this.myTextBox.Text = "Hello, World!";
};
Step 1: 匿名方法(使用delegate)的用法 => 該方法己過時
Modify from
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.myButton.Click += myButton_Click;
}
void myButton_Click(object sender, RoutedEventArgs e)
{
this.myTextBox.Text = "Hello, World!";
}
}
To
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//使用"匿名方法" => 該方法己過時
this.myButton.Click += delegate(object sender, RoutedEventArgs e)
{
this.myTextBox.Text = "Hello, World!";
};
}
}
Step 2: 改寫為 Lambda 表達式
1. 將 "delegate" 拿掉。2. 將 (
3. 承上,在後面加上Lambda表示式 "=>"。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//Lambda表達式
this.myButton.Click += ( sender, e) =>
{
this.myTextBox.Text = "Hello, World!";
};
}
}
For test UI (WPF)
<Window x:Class="Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Background="AliceBlue">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="2"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBox x:Name="myTextBox" Grid.Row="0"/>
<Button x:Name="myButton" Content="Click Me!" Grid.Row="2"/>
</Grid>
</Window>
https://www.youtube.com/watch?v=LA8V9DlLJ10
沒有留言:
張貼留言