Asked by:
reading Slider value creashes App

Question
-
Hey there,
I create a Slider trough XAML and I want to use the Slider's Value together with some other Slider's Value whenever one of them is changed. I added the ValueChanged Event to my Sliders and gave them both the same function to call in which I tried to read out each Sliders' value thus to progress it. Whenever I change one of the Sliders' value the App crashes.
I can only read out the Value of a slider by using the event handler ( e.NewValue) but I need to acces both at a time and I really don't know what the problem there might be.
Maybe I don't get some fundamental rule or behaviour or something so it would be really nice if you could tell me what causes this problem.
Thanks,
Patrik
- Edited by Partik Monday, August 26, 2013 5:09 PM
Monday, August 26, 2013 5:04 PM
All replies
-
Without code to repro this it is difficult to tell. Perhaps the function signatures are different. Create separate event handlers and from THEM call the single common function.
Jeff Sanders (MSFT)
@jsandersrocks - Windows Store Developer Solutions @WSDevSol
Getting Started With Windows Azure Mobile Services development? Click here
Getting Started With Windows Phone or Store app development? Click here
My Team Blog: Windows Store & Phone Developer Solutions
My Blog: Http Client Protocol Issues (and other fun stuff I support)Monday, August 26, 2013 7:20 PMModerator -
XAML:
<Slider x:Name="blankHueSlider" Maximum="360" Orientation="Horizontal" TickFrequency="30" TickPlacement="TopLeft" StepFrequency="10" ValueChanged="blankHueSlider_ValueChanged"/> <Slider x:Name="blankSatSlider" Maximum="1" Orientation="Horizontal" TickFrequency="0.1" Value="1" StepFrequency="0.05" ValueChanged="blankSatSlider_ValueChanged"/> <Slider x:Name="blankValSlider" Maximum="1" Orientation="Horizontal" TickFrequency="0.1" Value="1" StepFrequency="0.05" ValueChanged="blankValSlider_ValueChanged"/>
CS:
private void blankHueSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { int r, g, b; ColorExt MyColor = new ColorExt(); MyColor.HsvToRgb(blankHueSlider.Value, blankSatSlider.Value, blankValSlider.Value, out r, out g, out b); blankSampleRectangle.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(r), (byte)(g), (byte)(b))); }
I tried to walk the Debugger through the code step by step to know exactly what goes wrong and as soon as it tries to perform the line in which all the slider values are used, it crashes. I even cut everything down to just 1 line setting a textbox's text to be the value of the slider -> Crash.
The error is something like that i wasn't precise enough about which instance of the element I tried to call or so, haven't got it here atm.
Monday, August 26, 2013 7:30 PM -
Your code looks superficially correct given that you haven't provided details on the ColorExt class or its HsvToRgb method and we don't yet know the exact error.
To continue narrowing it down I'd pull the slider Value assignments into separate lines so it is easier to tell if the problem is in accessing the Values or in passing them to HsvToRgb.
Something else you might consider is databinding the values rather than handling them with separate ValueChanged methods for each Slider, but that is more of an architectural discussion than a tactical fix to what you're running in.
--Rob
Monday, August 26, 2013 11:38 PMModerator -
Thanks for your reply Rob. So this is the error:
+ Exception {"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException}
I tried the following to Isolate the error:
private void blankHueSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{label1.Text = blankHueSlider.Value.ToString();
}
-> same error, whereas
private void blankHueSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { label1.Text = e.NewValue.ToString(); }
works.
The ColorExt Class and ist functions have nothing to do with this error.
Wednesday, August 28, 2013 12:37 PM -
Need to Push this at this state:
I feel like it's getting worse. I tried to seperate all of the above by introducing new variables that hold the sliders' value and are updated via the ValueChanged Event (blankHueSliderValue = e.NewValue;)
This works, but now I am unable to update the rectangle's color. I get the same error:
+ sender {LEDcontroller.App} object {LEDcontroller.App}
- e {Windows.UI.Xaml.UnhandledExceptionEventArgs} Windows.UI.Xaml.UnhandledExceptionEventArgs
+ Exception {"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException}
All I try to do is this : blankSampleRectangle.Fill = newSolidColorBrush( Colors.Blue);
I have absolutely no idea what might cause this and I have no idea for a workaround or anything.
I can provide the source if you may need it. Thank you!
- Edited by Partik Monday, September 2, 2013 10:47 AM
Monday, September 2, 2013 10:46 AM