0

我正在尝试制作一个新的时间表应用程序,用户在其中输入 rowNo 和 colNo。到目前为止,我已经完成了这段代码,但是每当我尝试从数组中检索文本框时,都会出现 Null 引用异常。

XAML

<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" Background="{x:Null}">
    <Grid Name="bigGrid" Height="780" Width="1300">
        <Grid.Background>
            <ImageBrush Stretch="Fill" ImageSource="/TimeTableBackground.jpg"/>
        </Grid.Background>
        <StackPanel Name="panel" Height="128" Margin="0,-3,0,0" VerticalAlignment="Top" Background="#1D000000">
            <TextBlock TextWrapping="Wrap" Text="Time Table" FontSize="24" FontStyle="Italic" FontFamily="Segoe WP" FontWeight="Bold" Foreground="#FFC83DA2"/>
            <TextBlock TextWrapping="Wrap" Text="Edit Table" Height="72" Padding="0" FontSize="64" FontFamily="Comic Sans MS" FontStyle="Italic" Foreground="#FFFF6BD7"/>
        </StackPanel>
        <Grid Name="grid1" HorizontalAlignment="Stretch" Height="{Binding ElementName=LayoutRoot,Path=ActualHeight}" Width="5000" VerticalAlignment="Bottom" Background="#1C000000" Margin="-14,0,14,19" ShowGridLines="True" />
    </Grid>
</ScrollViewer>
<!--Sample code showing usage of ApplicationBar-->
 <phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="/Images/appbar.edit.rest.png" Text="Button 1" Click="ApplicationBarIconButton_Click"/>
        <shell:ApplicationBarIconButton IconUri="/Images/appbar.save.rest.png" Text="Button 2"/>
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
            <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>

XAML 只是为了知道控件的名称。但是,C# 代码就是其中之一。

C#

public partial class Edit_page : PhoneApplicationPage
{
    private TextBox[,] tbArray = new TextBox[9,9];
    int rowNo = 6, colNo = 6;
    public Edit_page()
    {

        InitializeComponent();
        //for loops to add rows and columns to the main grid
        for (int j = 0; j <= rowNo; j++)
        {
            RowDefinition Row = new RowDefinition();
            grid1.RowDefinitions.Add(Row);
            Row.MinHeight = 80;
        }
        for (int q = 0; q <= colNo; q++)
        {
            ColumnDefinition Col = new ColumnDefinition();
            grid1.ColumnDefinitions.Add(Col);
            Col.MinWidth = 128;
        }
        grid1.Width = 128 * (colNo);
        //for loop to add a text block to each cell.
        for (int h = 1; h <= rowNo; h++)
        {
            tbArray[h, 0] = new TextBox() { TextWrapping = TextWrapping.Wrap, AcceptsReturn = Convert.ToBoolean(1) };
            tbArray[h, 0].InputScope = new InputScope { Names = { new InputScopeName { NameValue = InputScopeNameValue.Text } } };
            tbArray[h, 0].Background = new SolidColorBrush(Colors.Red);
            tbArray[h, 0].SetValue(Grid.RowProperty, h);
            tbArray[h, 0].SetValue(Grid.ColumnProperty, 0);
            grid1.Children.Add(tbArray[h, 0]);
        }
        for (int h = 1; h <= colNo; h++)
        {
            tbArray[0,h] = new TextBox() { TextWrapping = TextWrapping.Wrap, AcceptsReturn = Convert.ToBoolean(1) };
            tbArray[0, h].InputScope = new InputScope { Names = { new InputScopeName { NameValue = InputScopeNameValue.Text } } };
            tbArray[0, h].Background = new SolidColorBrush(Colors.Red);
            tbArray[0, h].SetValue(Grid.RowProperty, 0);
            tbArray[0, h].SetValue(Grid.ColumnProperty, h);
            grid1.Children.Add(tbArray[0, h]);
        }
        for (int i = 1; i <= rowNo; i++)
        {
            for (int k = 1; k <= colNo; k++)
            {
                tbArray[i, k] = new TextBox() { TextWrapping = TextWrapping.Wrap, AcceptsReturn = Convert.ToBoolean(1) };
                tbArray[i, k].InputScope = new InputScope { Names = { new InputScopeName { NameValue = InputScopeNameValue.Text } } };
                //name each textbox in the grid
                tbArray[i, k].Name = string.Format("tb{0}{1}", i, k);
                tbArray[i, k].Text = string.Format("afandem{0}{1}", i, k);
                //ScrollViewer scroll = new ScrollViewer();
                //scroll.SetValue(Grid.RowProperty, i);
                //scroll.SetValue(Grid.ColumnProperty, k);
                //scroll.Content = tb;
                tbArray[i, k].SetValue(Grid.RowProperty, i);
                tbArray[i, k].SetValue(Grid.ColumnProperty, k);
                grid1.Children.Add(tbArray[i, k]);
            }
        }
    }

    private void ApplicationBarIconButton_Click(object sender, EventArgs e)
    {
        //var isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        ////For loops to create files
        //    for (int i = 0; i <= rowNo; i++)
        //    {
        //        for (int j = 0; j <= colNo; j++)
        //        {
        //            string fileName = string.Format("{0}{1}.txt", i, j);
        //            using (var file = isoStore.OpenFile(fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
        //            {
        //                using (var writer = new StreamWriter(file))
        //                {
        //                    writer.Write(tbArray[i, j].Text);
        //                }

        //            }
        //        }
        //    }
        grid1.Visibility = Visibility.Collapsed;
        Grid testGrid = new Grid();
        bigGrid.Children.Add(testGrid);
        for (int j = 0; j <= rowNo; j++)
        {
            RowDefinition Row = new RowDefinition();
            testGrid.RowDefinitions.Add(Row);
            Row.MinHeight = 80;
        }
        for (int q = 0; q <= colNo; q++)
        {
            ColumnDefinition Col = new ColumnDefinition();
            testGrid.ColumnDefinitions.Add(Col);
            Col.MinWidth = 128;
        }
        testGrid.Width = 128 * (colNo);
        for (int i = 0; i <= rowNo; i++)
        {
            for (int j = 0; j <= colNo; j++)
            {
                TextBlock tB = new TextBlock();
                tB.Text = tbArray[i, j].Text;
                tB.SetValue(Grid.RowProperty, i);
                tB.SetValue(Grid.ColumnProperty, j);
                testGrid.Children.Add(tB);

            }
        }
    }
}
4

1 回答 1

2

当您填充时tbArray,您正在使用以下循环

for (int h = 1; h <= rowNo; h++) { ... }
for (int h = 1; h <= colNo; h++) { ... }

由于在这两种情况下h都被初始化为1,因此元素没有被初始化并且是; 因此.0tbArray[0,0]nullNullReferenceException

我不太确定你的应用程序应该做什么,但可能有更好的方法来添加项目,而不是像你在做的那样在代码隐藏中做所有事情。

例如,如果您尝试在用户单击应用程序栏按钮时添加其他行,那么您应该使用 aListBox而不是Grid. 然后ItemTemplateListBox包含一个Grid具有 6 列(或一个StackPanel具有水平方向的列)和一个TextBlock在每个单元格内。然后,您可以将每个TextBlockText属性绑定到一个ObservableCollection并简单地将新文本添加到所有集合中,这将自动更新 UI。

ObservableCollection如果你用谷歌搜索的话,有几个关于 XAML 绑定和 s 的教程。此外,除非您在其中的每一个中添加很少的文本,否则TextBlock6 列对于手机屏幕来说太宽了。

于 2011-03-30T02:10:42.100 回答