Slider绑定事件,初始化NullPointerException错误
最近刚刚接触Silverlight,随便在网上找了一个入门的博文http://www.cnblogs.com/Terrylee/archive/2008/03/07/Silverlight2-step-by-step-part3.html,在给Slider绑定事件的时候,出现了NullPointerException错误,
求教了其他了知道了里面的错误。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Documents; 8 using System.Windows.Input; 9 using System.Windows.Media; 10 using System.Windows.Media.Animation; 11 using System.Windows.Shapes; 12 13 namespace SLApplicationEx 14 { 15 public partial class MainPage : UserControl 16 { 17 public MainPage() 18 { 19 InitializeComponent(); 20 } 21 22 23 private void RedSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) 24 { 25 Color color = Color.FromArgb((byte)AlphaSlider.Value, (byte)RedSlider.Value, (byte)GreenSlider.Value, (byte)BlueSlider.Value); 26 27 PreviewColor.Fill = new SolidColorBrush(color); 28 HexColor.Text = color.ToString(); 29 } 30 31 32 } 33 }
因为在MainPage.xaml中,对Slider设置了初始值,所以初始化的时候,便会执行RedSlider_ValueChanged事件,而此时,
几个Slider实例(AlphaSlider,RedSlider,GreenSlider,BlueSlider)并没有初始化,都是null,所以,会在RedSlider_ValueChanged
内出错。
正确的做法可以有两种:
(一)不为Slider设置初始值
这样,在程序初始化的时候,RedSlider_ValueChanged事件就不会被触发。
1 <StackPanel Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" VerticalAlignment="Center"> 2 <TextBlock Text="Alpha" FontSize="12" Margin="10,15,0,0"/> 3 <Slider x:Name="AlphaSlider" Margin="20,0,10,0" Maximum="255" ValueChanged="RedSlider_ValueChanged"/> 4 <TextBlock Text="Red" FontSize="12" Margin="10,15,0,0"/> 5 <Slider x:Name="RedSlider" Margin="20,0,10,0" Maximum="255" ValueChanged="RedSlider_ValueChanged"/> 6 <TextBlock Text="Green" FontSize="12" Margin="10,15,0,0"/> 7 <Slider x:Name="GreenSlider" Margin="20,0,10,0" Maximum="255" ValueChanged="RedSlider_ValueChanged"/> 8 <TextBlock Text="Blue" FontSize="12" Margin="10,15,0,0"/> 9 <Slider x:Name="BlueSlider" Margin="20,0,10,0" Maximum="255" ValueChanged="RedSlider_ValueChanged"/> 10 </StackPanel>
(二)在MainPage构造函数中启用Loaded事件
因为给Slider设置了初始值,所以,在程序初始化前,就不应该绑定ValueChanged事件,故而,可以使用Loaded事件去绑定ValueChanged事件。
............. <StackPanel Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" VerticalAlignment="Center"> <TextBlock Text="Alpha" FontSize="12" Margin="10,15,0,0"/> <Slider x:Name="AlphaSlider" Margin="20,0,10,0" Maximum="255" Value="255" /> <TextBlock Text="Red" FontSize="12" Margin="10,15,0,0"/> <Slider x:Name="RedSlider" Margin="20,0,10,0" Maximum="255" Value="255" /> <TextBlock Text="Green" FontSize="12" Margin="10,15,0,0"/> <Slider x:Name="GreenSlider" Margin="20,0,10,0" Maximum="255" Value="102" /> <TextBlock Text="Blue" FontSize="12" Margin="10,15,0,0"/> <Slider x:Name="BlueSlider" Margin="20,0,10,0" Maximum="255" Value="0" /> </StackPanel> ...............
然后在MainPage.xaml.cs内加上Loaded事件
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Documents; 8 using System.Windows.Input; 9 using System.Windows.Media; 10 using System.Windows.Media.Animation; 11 using System.Windows.Shapes; 12 13 namespace SLApplicationEx 14 { 15 public partial class MainPage : UserControl 16 { 17 public MainPage() 18 { 19 InitializeComponent(); 20 21 this.Loaded += MainPage_Loaded;//绑定MainPage_Loaded事件 22 } 23 24 private void RedSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) 25 { 26 Color color = Color.FromArgb((byte)AlphaSlider.Value, (byte)RedSlider.Value, (byte)GreenSlider.Value, (byte)BlueSlider.Value); 27 28 PreviewColor.Fill = new SolidColorBrush(color); 29 HexColor.Text = color.ToString(); 30 } 31 32 void MainPage_Loaded(object sender, RoutedEventArgs e) 33 { 34 this.RedSlider.ValueChanged += RedSlider_ValueChanged; 35 } 36 } 37 }