0

好的,所以我有这段代码通常构建一个屏幕,然后在添加网格中的第 3 列之后,我打破了 for 循环,然后它看起来像这样。

代码:

 private void buildScreen()
 {
     try
     {
         foreach (var block in WorkflowBase.ViewModel_WFTransaction.Transaction.Form.Blocks.Where(item => item.blockType == AccTech.WP8.Workflow.DataModels.BlockAttributes.BlockType.DetailGrid))
         {
             myscroller = new ScrollViewer();
             myscroller.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
             myscroller.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
             myscroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;

             Grid grid = new Grid();
             grid.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
             grid.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;

             foreach (var field in block.Fields)
             {
                 if (field.PhoneField == true)
                 {
                     int count = block.Fields.IndexOf(field);
                     ColumnDefinition c1 = new ColumnDefinition();
                     //c1.Width = new GridLength(1000, GridUnitType.Star);                        
                     grid.ColumnDefinitions.Add(c1);

                     TextBlock headers = new TextBlock();
                     headers.Tap += headers_Tap;
                     headers.Text = field.FieldDescLoc.ToString();
                     headers.Name = field.ColumnName;
                     headers.Foreground = new SolidColorBrush(Colors.Black);
                     headers.FontSize = 22;
                     headers.TextWrapping = TextWrapping.Wrap;
                     headers.SetValue(Grid.ColumnProperty, count);
                     headers.Width = 100;
                     headers.Margin = new Thickness(0, 0, 10, 0);

                     if (field.FieldType == FieldAttributes.DataType.dtString)
                     {
                        headers.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                     }
                     else if (field.FieldType == FieldAttributes.DataType.dtInteger || field.FieldType == FieldAttributes.DataType.dtDate)
                     {
                        headers.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                     }
                     grid.Children.Add(headers);

                     foreach (var row in block.Rows)
                     {
                            RowDefinition r1 = new RowDefinition();
                            r1.Height = new GridLength(50, GridUnitType.Star);
                            grid.RowDefinitions.Add(r1);

                            int i = block.Rows.IndexOf(row);
                            int count2 = block.Fields.IndexOf(field);

                            TextBlock item = new TextBlock();
                            item.Margin = new Thickness(5, 5, 5, 10);
                            item.Text = block.Rows[i].getKeyValue(field.ColumnName);
                            item.SetValue(Grid.RowProperty, i + 1);
                            item.SetValue(Grid.ColumnProperty, count2);

                            if (field.FieldType == FieldAttributes.DataType.dtString)
                            {
                                item.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                            }
                            else if (field.FieldType == FieldAttributes.DataType.dtInteger || field.FieldType == FieldAttributes.DataType.dtDate)
                            {
                                item.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                            }
                            grid.Children.Add(item);
                        }

                        if (this.Orientation == PageOrientation.Portrait || this.Orientation == PageOrientation.PortraitDown || this.Orientation == PageOrientation.PortraitUp)
                        {
                            if (count >= 3)
                            {
                                myscroller.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
                                break;
                            }
                        }
                    }
                }                   
                myscroller.Content = grid;
                this.PageTilte.Text = block.BlockName.ToString();
                //ParentControl.Children.Clear();
                this.ParentControl.Children.Add(myscroller);                  
            }

屏幕看起来像这样:

详细画面

这样工作正常,现在我将屏幕侧向切换到横向视图,这就是代码。

    private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
    {
        this.ParentControl.Children.Clear();
        buildScreen();            
    }

如您所见,我清除了父控件,然后我再次调用该方法来构建屏幕!

这是我的错误。 在此处输入图像描述

而内在的例外是空的!谁能告诉我这里有什么问题?

4

1 回答 1

0

我找到了!,经过几分钟的仔细阅读!,我发现我的 textblock.Name 属性变为空,所以只需进行空检查并将名称命名为“”,它就可以了!

 TextBlock headers = new TextBlock();
                        headers.Tap += headers_Tap;
                        headers.Text = field.FieldDescLoc.ToString();
                        //headers.Name = field.FieldDescLoc;

                        if (!string.IsNullOrEmpty(field.ColumnName))
                            headers.Name = field.ColumnName;
                        else
                            headers.Name = field.FieldDesc;
于 2013-07-16T09:05:04.037 回答