开发者

Silverlight Why doesn't this work

I am trying to create a slider(without binding). Currently i did this:

Xaml:

<Slider Height="68" HorizontalAlignment="Left" Margin="52,45,0,0" x:Name="slider1" VerticalAlignment="Top" Width="256" Minimum="1" Maximum="40" Value="10" ValueChanged="slider1_ValueChanged" />
    <TextBlock x:Name="textBlock1" Margin="52,120,0,0" Text="Slide it!" ></TextBlock>

And in my cs:

private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
  textBloxk1.FontSize = slider1.Value;
}

But the silverlight page keeps load开发者_开发技巧ing and won't show the slider, anyone know what I'm doing wrong??


Probably at first ValueChanged event, slider1 and textblock1 are still null. try this:

private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
            if (textBlock1 != null && slider1 != null)
            {
                textBlock1.FontSize = slider1.Value;
            }
}


look at your Xaml.. you setting value to 10 Value="10"... but at that time textBlock dosn't exist.. be carefull..

when parser parse Xaml it first create Slider then sets all values to slider (and fire all attached events), and only then it creates TextBlock...

so change you code to this, and everithing should be fine..

    private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        if (textBlock1 != null && slider1 != null)
        {
            textBlock1.FontSize = slider1.Value;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜