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

发布于 2024-12-18 07:29:56 字数 4182 浏览 8 评论 0原文

我正在尝试在两个枢轴项目中添加边框。

当我的边框第一次添加到数据透视项中的网格时,一切正常。但是当我尝试在同一枢轴项目中第二次添加边框时,它会抛出异常“参数不正确” 这是我的代码:

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

        }

此代码第一次工作,但之后出现异常,并且 ContentPanel2011 即ivot_item1 没有填充 border2011

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

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

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

发布评论

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

评论(1

寄意 2024-12-25 07:29:56

完成了。
只需在再次设置内容之前将枢轴上的内容属性设置为 null 即可。

我刚刚添加:

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

pivot_item1Loaded()pivot_item2Loaded()

,它工作正常。

It is done.
just set content property on pivots to null before before setting the content again.

I have just added:

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

in pivot_item1Loaded() and pivot_item2Loaded()

and it is working fine.

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