简体   繁体   English

为什么我的XAML控件为null?

[英]Why are my XAML controls null?

I have a XAML layout similar to this: 我有一个类似于以下内容的XAML布局:

<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. 在C#文件的代码隐藏中,我使用这些控件从TextBox中获取一个双精度,从ComboBoxes中获取一些更多的整数,然后使用来自控件的数据创建一个计算器类型的对象。 I make the calculation and display the results in some other TextBlocks. 我进行计算并在其他一些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. 当我运行该程序时,我遇到一个空指针异常,该异常在尝试访问TextBox的text属性时引发。 It turns out that all of the XAML controls are null. 事实证明,所有XAML控件都是null。 However, _contentLoaded is set to true and the code definition for this.IntializeComponent looks correct behind the scenes. 但是,_contentLoaded设置为true,并且此代码定义。IntializeComponent在幕后看起来正确。

Why are all my controls set to null when it seems like everything is working correctly? 当看起来一切正常时,为什么将所有控件都设置为null? 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(); Calc x =新的Calc();
  2. this.InitializeComponent(); this.InitializeComponent();

Calc() was first than InitializeComponent(), but InitializeComponent() create your controls. Calc()首先于InitializeComponent(),但InitializeComponent()创建您的控件。

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. 类初始化时,我的一些TextBox控件也遇到相同的问题。 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. 我要做的并不是真正完美的解决方案,因为在类运行时并非所有控件(TextBox,ComboBox,RadioButton等)都为空,并且代码,应用程序或VS中发生了某些情况。丢失或做错了...。但是至少现在可以正常工作了。 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. 对于我的英语不好,如果我有错误,我表示歉意,这不是我的母语。

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

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