user1171498
user1171498

Reputation: 23

Why and how to get rid of NullReferenceException

namespace WindowsPhoneApp
{

    Class MainPage()
    {
        private void ProcentSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            ShowSliderValue.Text = ProcentSlider.Value.ToString(); //<-- NullReferenceException
        }

    }
}

I can't reference the value of the Slider control to the TextBox control...

Upvotes: 0

Views: 533

Answers (1)

DaveDev
DaveDev

Reputation: 42185

Try this - it might be because the ShowSliderValue is running in a different thread.. bit of a guess though.

private void ProcentSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    Dispatcher.BeginInvoke(new Action(() => ShowSliderValue.Text = ProcentSlider.Value.ToString();));
}

or maybe this?

private void ProcentSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    var slider = (ProcentSlider)sender;
    ShowSliderValue.Text = slider.Value.ToString();
}

Upvotes: 1

Related Questions