Windows Phone 7 时间表程序

发布于 2024-10-27 07:17:39 字数 6585 浏览 1 评论 0原文

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

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);

            }
        }
    }
}

I am trying to make a new Time table app, where the user enters rowNo and colNo.
I have done this code so far, but I get a Null reference exception, whenever I try to retrieve a textbox from the array.

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 is just to know the names of controls. However, C# code is the one.

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);

            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薯片软お妹 2024-11-03 07:17:39

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

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

,因为 h 被初始化为 1 而不是 0在这两种情况下,tbArray[0,0] 元素都不会被初始化,并且为 null;因此出现 NullReferenceException。

我不太确定您的应用程序应该做什么,但可能有一种更好的方法来添加项目,而不是像您一样在代码隐藏中执行所有操作。

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

如果您 Google 一下,可以找到一些关于 XAML 绑定和 ObservableCollection 的教程。另外,除非您在每个 TextBlock 中放置很少的文本,否则 6 列对于手机屏幕来说太宽了。

When you're populating tbArray you're using the following loops

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

Since h is being initialized to 1 instead 0 in both cases, the tbArray[0,0] element does not get initialized and is null; hence the NullReferenceException.

I'm not really sure what your app is supposed to do but there probably is a better way to go about adding items than doing everything in code-behind as you're doing.

For instance, if you're trying to add additional rows when the user clicks an application bar button, then you should be using a ListBox instead of the Grid. The ItemTemplate of the ListBox would then contain a Grid having 6 columns (or a StackPanel with horizontal orientation), and a TextBlock within each cell. You can then bind each TextBlock's Text property to an ObservableCollection and simply add new text to all the collections, this will automatically update the UI.

There are several tutorials on XAML bindings and ObservableCollections if you Google it. Also, unless you're putting very little text in each of those TextBlocks, 6 columns is going to be too wide for a phone screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文