After the value is set in the XAML file and the solution is compiled, an error "object reference is not set to the instance of the object" is reported"
Scolour. XAML FileCode
Code
< Usercontrol X: Class = "Sample. scolour"
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: d = "Http://schemas.microsoft.com/expression/blend/2008"
Xmlns: MC = "Http://schemas.openxmlformats.org/markup-compatibility/2006"
MC: ignorable = "D"
D: designheight = "300" D: designwidth = "400" >
< Grid X: Name = "Layoutroot" Background = "White" >
< Grid. rowdefinitions >
< Rowdefinition Height = "120" />
< Rowdefinition Height = "120" />
</ Grid. rowdefinitions >
< Grid. columndefinitions >
< Columndefinition Width = "200" />
< Columndefinition Width = "*" />
</ Grid. columndefinitions >
< Rectangle Grid. Row = "0" Grid. Column = "1" X: Name = "Previewcolour" Fill = "# Ffff6600" Margin = "15" > </ Rectangle >
< Stackpanel Grid. Row = "1" Grid. Column = "1" >
< Textblock Fontsize = "12" > Color </ Textblock >
< Textbox X: Name = "Hexcolour" Text = "# Ffff6600" Height = "30" Width = "160" Margin = "5" Horizontalalignment = "Left" Fontsize = "11" > </ Textbox >
</ Stackpanel >
< Stackpanel Grid. Row = "0" Grid. Column = "0" Grid. rowspan = "2" Verticalalignment = "Center" >
< Textblock Text = "Alpha" Fontsize = "11" Margin = "10, 15, 0, 0" />
< Slider X: Name = "Slideralpha" Margin = "20, 0, 10, 0" Maximum = "255" Value = "255" Valuechanged = "Slider_valuechanged" />
< Textblock Text = "Red" Fontsize = "11" Margin = "10, 15, 0, 0" />
< Slider X: Name = "Sliderred" Margin = "20, 0, 10, 0" Maximum = "255" Value = "255" Valuechanged = "Slider_valuechanged" />
< Textblock Text = "Green" Fontsize = "11" Margin = "10, 15, 0, 0" />
< Slider X: Name = "Slidergreen" Margin = "20, 0, 10, 0" Maximum = "255" Value = "102" Valuechanged = "Slider_valuechanged" />
< Textblock Text = "Blue" Fontsize = "11" Margin = "10, 15, 0, 0" />
< Slider X: Name = "Sliderblue" Margin = "20, 0, 10, 0" Maximum = "255" Value = "0" Valuechanged = "Slider_valuechanged" />
</ Stackpanel >
</ Grid >
</ Usercontrol >
Scolour. XAML. CS File Code
Code
1 Using System. Windows. documents;
2 Using System. Windows. input;
3 Using System. Windows. Media;
4 Using System. Windows. Media. animation;
5 Using System. Windows. shapes;
6 Namespace Sample
7 {
8 Public Partial Class Scolour: usercontrol
9 {
10 Public Scolour ()
11 {
12 Initializecomponent ();
13 }
14 Private Void Slider_valuechanged ( Object Sender, routedpropertychangedeventargs < Double > E)
15 {
16 Color mycolor = Color. fromargb (( Byte ) Slideralpha. Value ,( Byte ) Sliderred. Value ,( Byte ) Slidergreen. Value ,( Byte ) Sliderblue. value );
17 Previewcolour. Fill = New Solidcolorbrush (mycolor );
18 Hexcolour. Text = Mycolor. tostring ();
19 }
20 }
21 }
Check the code carefully. It seems that there are no errors, but the system always prompts "the object is not referenced to the instance of the object ". Later, after checking the information, this may be a bug in Silverlight. If valuechanged is defined, it seems that the value attribute of Slider cannot be assigned an initial value in XAML. We can assign values to the scolour constructor:
1 Public Scolour ()
2 {
3 Initializecomponent ();
4 This . Slideralpha. Value = 255 ;
5 This . Sliderred. Value = 255 ;
6 This . Slidergreen. Value = 102 ;
7 This . Sliderblue. Value = 0 ;
8 }
Running interface, a simple Color Filter