简体   繁体   中英

Why are my XAML controls null?

I have a XAML layout similar to this:

<Grid>
    <TextBox x:Name="inputTextBox" LostFocus="inputTextBox_LostFocus" TextChanged="inputTextBox_TextChanged" GotFocus="inputTextBox_GotFocus" />
    <ComboBox x:Name="inputComboBox" SelectionChanged="inputComboBox_SelectionChanged">
        <ComboBoxItem IsSelected="True">10</ComboBoxItem>
        <ComboBoxItem>15</ComboBoxItem>
        <ComboBoxItem>20</ComboBoxItem>
    </ComboBox>
    <ComboBox x:Name="inputComboBoxTwo" SelectionChanged="inputComboBoxTwo_SelectionChanged">
        <ComboBoxItem IsSelected="True">1</ComboBoxItem>
        <ComboBoxItem>2</ComboBoxItem>
        <ComboBoxItem>3</ComboBoxItem>
    </ComboBox>
</Grid>

Pretty simple. In the codebehind C# file, I use these controls to take in a double from the TextBox, some more ints from the ComboBoxes, then I create a calculator type object with the data from the controls. I make the calculation and display the results in some other TextBlocks.

namespace TipCalc
{
    public sealed partial class MainPage : Page
    {
        Calc x = new Calc();

        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
        }

        //
        //Appropriate event handlers from XAML controls that all call the calculation method.
        //

        private void calcIt()
        { 
            x.amt = double.Parse(inputTextBox.Text);
            x.cal1 = int.Parse(inputComboBox.SelectedItem.ToString());
            x.cal2 = int.Parse(inputComboBoxTwo.SelectedItem.ToString());

            //Send calculated values to output TextBlocks.
        }
    }
}

When I run this program, I hit a null pointer exception that is thrown when I attempt to access the text property of the TextBox. It turns out that all of the XAML controls are null. However, _contentLoaded is set to true and the code definition for this.IntializeComponent looks correct behind the scenes.

Why are all my controls set to null when it seems like everything is working correctly? Is there a way to manually initialize them if they aren't correctly being initialized automatically? Am I doing anything wrong?

the code run like:

  1. Calc x = new Calc();
  2. this.InitializeComponent();

Calc() was first than InitializeComponent(), but InitializeComponent() create your controls.

you can change to:

Calc x;

public MainPage()
{
    this.InitializeComponent();
    x = new Calc();
    this.NavigationCacheMode = NavigationCacheMode.Required;
}

I have the same problem with some of my TextBox controls when the class initializes. What I did to solve this is not the real and perfect solution because not all the controls (TextBox, ComboBox, RadioButton, etc) are null when the class is running, and there's something happening in my code or my app or my VS that I'm missing or doing wrong.... But at least is working fine now. I hope is useful to you:

if(TextBox1 == null)
{
     //I'm re-initializing the control because is null
     TextBox1 = new TextBox();
}

For your code it should be something like this:

if(inputTextBox == null)
{
     inputTextBox.Text = new TextBox();
}

x.amt = double.Parse(inputTextBox.Text);

I hope this 'solution' is good enough for you. And for my poor English I apologize if I have mistakes, is not my native language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM