简体   繁体   English

在后面的代码中设置TextBlock文本将提供NullReferenceException

[英]Setting TextBlock Text in Code Behind Gives NullReferenceException

I have a TextBlock control on my page in which I would like to set the value in the code behind, but I am getting a NullReferenceException . 我的页面上有一个TextBlock控件,我想在其中的代码中设置该值,但是却收到了NullReferenceException The value of the TextBlock changes according to the current position of a Slider Control. TextBlock的值根据滑块控件的当前位置而变化。 To note, the page never gets fully NavigatedTo when the error occurs. 注意,发生错误时,页面永远不会完全导航到。 I'm not sure what I can do to solve this, any ideas? 我不确定该如何解决,有什么想法吗?

XAML XAML

<TextBlock x:Name="OpacityNumberTextBlock" HorizontalAlignment="Center">
<Slider x:Name="MenuOpacitySlider"  Minimum="1" Maximum="6" Margin="12,20,12,0" 
                    ValueChanged="MenuOpacitySlider_ValueChanged" Value="1"/>

Code Behind 背后的代码

public void MenuOpacitySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        Slider slider = sender as Slider;

        //Round the value so it is a whole number even when the slider is dragged
        slider.Value = Math.Round(slider.Value);

        num = (int)slider.Value;

        switch (num)
        {
            case 1:
                OpacityNumberTextBlock.Text = "1"; //NullReferenceException
                break;
            ..
        }
    }

Your ValueChanged is fired in InitializeComponent() while the TextBlock is null. TextBlock为null时,在InitializeComponent()触发ValueChanged Subscribe after InitializeComponent() : InitializeComponent()之后订阅:

In XAML: 在XAML中:

<TextBlock x:Name="OpacityNumberTextBlock" HorizontalAlignment="Center"/>
<Slider x:Name="MenuOpacitySlider"  Minimum="1" Maximum="6" Margin="12,20,12,0" Value="1"/>

In code behind: 在后面的代码中:

public MainPage()
{
    InitializeComponent();
    MenuOpacitySlider.ValueChanged+=MenuOpacitySlider_ValueChanged;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 设置文本或图像会产生NullReferenceException - Setting Text or Image gives NullReferenceException WPF的新增功能-在导致NullReferenceException的背后更新代码中的TextBlock - New to WPF - Updating TextBlock in Code behind causing NullReferenceException 设置 WPF 的 Textblock.Text 属性会引发 NullReferenceException - Setting WPF's Textblock.Text property throws NullReferenceException 设置后面的TextBlock的宽度,高度为variablesizedwrapgrid无法正确显示 - Setting width, height of TextBlock behind code for variablesizedwrapgrid not displaying correctly 从代码代码后面的文本到具有不同字体样式的文本块中 - Text from Code Code Behind into Textblock with Different Font Style 如何在后台代码中将Unicode字符设置为TextBlock的Text? - How to set Unicode character as Text of TextBlock in the code-behind? 如何在代码隐藏中从DataTemplate内部的TextBlock获取文本 - How to get the Text from TextBlock inside a DataTemplate in code-behind 将WPF文本设置为TextBlock - Setting WPF text to TextBlock 在后面的代码中将样式设置为文本块 - Set style to textblock in code behind 将TextBox文本分配给TextBlock-System.NullReferenceException:“未设置对象引用……” - Assign TextBox text to TextBlock - System.NullReferenceException: „Object reference not set …"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM