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

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

问题描述

我收到值没有在预期范围内的除外,并称
的儿童在堆栈面板。这种情况即使在myStackPanel.Children.Count = 0加入的StackPanel之前。 ?任何想法,为什么

 无效FUNC()
{
myStackPanel.Children.Clear();
名单,LT;文档和GT; lDocs = docDictionary [ID]
的foreach(在lDocs文档LDOC)
{
...
边境myTextborder =新的边界();
myTextborder.BorderThickness =新厚度(1);
myTextborder.Name = lDoc.Name;
...

myStackPanel.Children.Add(myTextborder); //获得价值并不在预期范围之内的异常这里
}
}

FUNC()被调用多次。我读到,当我们试图用相同的名称添加儿童发生错误。但对我来说,我清除栈面板,即使在foreach循环运行每次通话只有一次的FUNC()


解决方案

当有添加具有相同名称的两个元素,可导致此错误。根据你的情况,是否有任何重复lDoc.Name值?如果是这样,你可以添加一个额外的唯一标识符。例如:

  INT ID = 0; //外foreach循环

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


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

解决方案

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天全站免登陆