在Silverlight宣告事件的方式有兩種:
- 直接在XAML內部宣告 (優點:簡單明瞭, 缺點: 只能使用預設的參數, 不能再加參數)
- 在.cs裡面作宣告 (優點: 彈性大, 可以做變化; 缺點: 比較困難(吧~) )
第一種方式:
在XAML直接作宣告(eg. 在Page.xaml):
<Canvas Loaded=”Canvas_Loaded”>
<Button x:Name=”myButton” Content=”Hello”
Canvas.Left=”10″ Canvas.Top=”10″ />
</Canvas>
在Page.xaml.cs作處理:
private void Canvas_Loaded(object sender, RoutedEventArgs e)
{
theButton.Content = “Please Click Me”;
}
第二種方式
不需在XAML裡面宣告事件
<!– <Canvas Loaded=”Canvas_Loaded”> –>
<Canvas >
<Button x:Name=”theButton” Content=”I’m Indented!”
Canvas.Left=”150″ Canvas.Top=”20″ />
<CheckBox x:Name=”RushOrder” Content=”Rush”
Canvas.Left=”50″ Canvas.Top=”20″ FontSize=”18″ />
</Canvas>
而是直接在Page.xaml.cs的Page( ) Constructor動手腳~
public Page()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
/*throw new NotImplementedException(); 去掉這行 */
theButton.Click +=new RoutedEventHandler(theButton_Click); //事件1
RushOrder.Checked += new RoutedEventHandler(RushOrder_Changed); //事件2
RushOrder.Unchecked +=new RoutedEventHandler(RushOrder_Change); //事件3
}
void RushOrder_Changed(object sender, RoutedEventArgs e)
{
if (RushOrder.IsChecked == true)
{
RushOrder.Content = “RUSH”;
}
else
{
RushOrder.Content = “Rush”;
}
}
發佈留言