Silverlight:值不在预期范围内异常 [英] Silverlight: Value does not fall within the expected range exception

查看:31
本文介绍了Silverlight:值不在预期范围内异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在添加时收到值不在预期范围内的异常"儿童堆叠面板.即使在添加到堆栈面板之前 myStackPanel.Children.Count = 0 也会发生这种情况.知道为什么吗?

I am getting "Value does not fall within the expected range exception" when adding children to stack panel. This happens even when myStackPanel.Children.Count = 0 just before adding to stackpanel. Any idea why?

void func()
{
          myStackPanel.Children.Clear();        
          List<Docs> lDocs =  docDictionary[ID];
          foreach (Docs lDoc in lDocs)
          {
                 ...
                 Border myTextborder = new Border();                   
                 myTextborder.BorderThickness = new Thickness(1);
                 myTextborder.Name = lDoc.Name;
                 ...

                 myStackPanel.Children.Add(myTextborder);   //Getting Value does not fall within the expected range exception here
          }
}

func() 被多次调用.我读到当我们尝试添加同名的孩子时会发生错误.但就我而言,我正在清除堆栈面板,即使每次调用 func() 时 foreach 循环只运行一次,也会发生错误

func() is called multiple times. I read that the error occurs when we attempt to add children with the same name. But in my case, I am clearing the stack panel and the error occurs even if the foreach loop runs just once per call to the func()

推荐答案

当添加两个同名元素时可能会导致此错误.在您的情况下,是否有任何重复的 lDoc.Name 值?如果是这样,您可以添加额外的唯一标识符.例如:

This error can be caused when there are two elements being added with the same name. In your case, are there any duplicate lDoc.Name values? If so, you could add an extra unique identifier. For example:

int id = 0; //outside foreach loop

myTextborder.Name = lDoc.Name + id.ToString();
id++;

这篇关于Silverlight:值不在预期范围内异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆