那啥,是从这里整理出来的,感谢Rising_Sun,整理的过于简单,看不明白的戳这里
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;namespace Delegate委托 {/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}delegate string SayMessage(string msg);static string SayHello(string Name){return string.Format("Hello {0}", Name);}private void button1_Click(object sender, RoutedEventArgs e){SayMessage say = new SayMessage(SayHello);textBlock1.Text = say("委托");}private void button2_Click(object sender, RoutedEventArgs e){SayMessage say = delegate(string Name){return string.Format("Hello {0}", Name);};textBlock2.Text = say("匿名方法");}private void button3_Click(object sender, RoutedEventArgs e){SayMessage say = (Name) =>{return string.Format("Hello {0}", Name);};textBlock3.Text = say("Lambda");}private void button4_Click(object sender, RoutedEventArgs e){Func<string, string> say1 = delegate(string Name){return string.Format("Hello {0}", Name);};//和Lambda 结合Func<string, string> say2 = (Name) =>{return string.Format("Hello {0}", Name);};textBlock4.Text = say1("Func 委托") + say2("Func+Lambda 委托");}private void button5_Click(object sender, RoutedEventArgs e){Action<string> say1 = delegate(string Name){textBlock5.Text = (string.Format("Hello {0}", Name));};//和Lambda 结合Action<string> say2 = (Name) =>{textBlock5.Text += (string.Format("Hello {0}", Name));};say1("Action 委托");say2("Action+Lambda 委托");}} }
下面那啥,不堪入目!没人看到没人看到没人看到。。。。。。
<Window x:Class="Delegate委托.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><Button Content="Button" Height="25" HorizontalAlignment="Left" Margin="34,12,0,0" Name="button1" VerticalAlignment="Top" Width="65" Click="button1_Click" /><TextBlock Height="25" HorizontalAlignment="Left" Margin="166,12,0,0" Name="textBlock1" Text="TextBlock" VerticalAlignment="Top" Width="160"/><Button Content="Button" Height="26" HorizontalAlignment="Left" Margin="34,58,0,0" Name="button2" VerticalAlignment="Top" Width="65" Click="button2_Click" /><TextBlock Height="26" HorizontalAlignment="Left" Margin="165,58,0,0" Name="textBlock2" Text="TextBlock" VerticalAlignment="Top" Width="161" /><Button Content="Button" Height="26" HorizontalAlignment="Left" Margin="34,108,0,0" Name="button3" VerticalAlignment="Top" Width="65" Click="button3_Click" /><TextBlock Height="26" HorizontalAlignment="Left" Margin="165,108,0,0" Name="textBlock3" Text="TextBlock" VerticalAlignment="Top" Width="161" TextWrapping="Wrap"/><Button Content="Button" Height="26" HorizontalAlignment="Left" Margin="34,158,0,0" Name="button4" VerticalAlignment="Top" Width="65" Click="button4_Click" /><TextBlock Height="26" HorizontalAlignment="Left" Margin="165,158,0,0" Name="textBlock4" Text="TextBlock" VerticalAlignment="Top" Width="161" TextWrapping="Wrap" /><Button Content="Button" Height="26" HorizontalAlignment="Left" Margin="34,208,0,0" Name="button5" VerticalAlignment="Top" Width="65" Click="button5_Click" /><TextBlock Height="26" HorizontalAlignment="Left" Margin="165,208,0,0" Name="textBlock5" Text="TextBlock" VerticalAlignment="Top" Width="161" TextWrapping="Wrap"/></Grid> </Window>
咳咳,反正是自己看的