时间表程序windows phone 7

时间:2011-03-30 01:46:23

标签: silverlight windows-phone-7 time schedule xmltable

我正在尝试创建一个新的Time表应用,用户输入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);

            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

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

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

由于h在两种情况下都被1初始化为0,因此tbArray[0,0]元素未初始化且为null;因此NullReferenceException

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

例如,如果您在用户点击应用栏按钮时尝试添加其他行,那么您应该使用ListBox而不是GridItemTemplate的{​​{1}}将包含一个ListBox,其中包含6列(或Grid水平方向),每个单元格中有一个StackPanel。然后,您可以将每个TextBlock的{​​{1}}属性绑定到TextBlock,只需向所有集合添加新文本,这将自动更新UI。

如果您使用Google,则有几个关于XAML绑定和Text的教程。此外,除非您在每个ObservableCollection中放置非常少的文字,否则6列对于手机屏幕来说太宽了。