简体   繁体   English

Grid.Add.Children(UIElement)返回参数不正确的异常

[英]Grid.Add.Children(UIElement) returns Parameter is incorrect exception

I am trying to add border in two pivot items. 我正在尝试在两个枢纽项目中添加边框。

When my border is added to grid in pivot item for the first time everything is working fine. 第一次将我的边框添加到数据透视表项的网格中时,一切正常。 But when i try to add border second time in same pivot item it throws an exception "The parameter is incorrect" here is my code : 但是,当我第二次尝试在同一数据透视表项中添加边框时,它抛出异常“参数不正确”,这是我的代码:

private void pivot_item1Loaded()
        {
            WebClient webClient2011 = new WebClient();
            string Url2011 = "http://hostname/Details/Images?year=2011" + "&time=" + System.DateTime.UtcNow;
            webClient2011.OpenReadAsync(new Uri(Url2011));

            webClient2011.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted2011);
        }

        private void pivot_item2Loaded()
        {
            WebClient webClient2012 = new WebClient();
            string Url2012 = "http://hostname/Details/Images?year=2012" +"&time="+ System.DateTime.UtcNow;
            webClient2012.OpenReadAsync(new Uri(Url2012));

            webClient2012.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted2012);
        }
        public void webClient_OpenReadCompleted2011(object sender, OpenReadCompletedEventArgs e)
        {
            StringBuilder output = new StringBuilder();
            try
            {               
                using (XmlReader reader = XmlReader.Create(e.Result))
                {

                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            if (reader.Name == "iconPath")
                            {
                                string iconPath = reader.ReadElementContentAsString();
                                iconImages2011.Add(iconPath);
                            }
                            if (reader.Name == "imagePath")
                            {
                                string imagePath = reader.ReadElementContentAsString();
                                fullScreenImages2011.Add(imagePath);
                            }
                        }

                    }
                }

                int numOfRows = (iconImages2011.Count) / 3 + 1;
                for (int j = 0; j < numOfRows; j++)
                {
                    //pivot_item1 
                    ContentPanel2011.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(150) });
                }
                int rowCount = 0;
                int columnCount = 0;

                for (int i = 0; i < iconImages2011.Count; i++)
                {
                    Border border2011 = new Border();
                    border2011.Background = new SolidColorBrush(Colors.Blue);

                    border2011.Height = 110;
                    border2011.Width = 110;
                    border2011.CornerRadius = new CornerRadius(10);

                    Canvas canvas2011 = new Canvas();
                    canvas2011.Height = 110;
                    canvas2011.Width = 110;

                    BitmapImage AppImage = new BitmapImage(new Uri(iconImages2011[i], UriKind.Absolute));

                    Image img = new Image();
                    img.Source = AppImage;
                    img.Width = 90;
                    img.Height = 90;
                    img.Stretch = Stretch.Fill;
                    img.Margin = new Thickness(10, 10, 10, 10);
                    canvas2011.Children.Add(img);
                    border2011.Child = canvas2011;
                    border2011.Name = i.ToString();

                    Grid.SetColumn(border2011, columnCount);
                    Grid.SetRow(border2011, rowCount);

                    ContentPanel2011.Children.Add(border2011);

                    pivot2011.Content = ContentPanel2011;
                    if (columnCount < 2)
                    {
                        columnCount++;
                    }
                    else if (columnCount == 2)
                    {
                        columnCount = 0;
                        rowCount++;
                    }

                }

            }

            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }

        }

This code works for the first time but gives exception after that and ContentPanel2011 viz pivot_item1 do not get filled with border2011 这段代码第一次起作用,但是在此之后给出了异常,并且ContentPanel2011也就是pivot_item1没有用border2011填充

It is done. 完成了 just set content property on pivots to null before before setting the content again. 只需在重新设置内容之前将数据透视表上的content属性设置为null即可。

I have just added: 我刚刚添加:

pivot2011.Content = null;
pivot2012.Content = null;

in pivot_item1Loaded() and pivot_item2Loaded() pivot_item1Loaded()pivot_item2Loaded()

and it is working fine. 而且工作正常。

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

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