长度不能小于零。 [英] Length cannot be less than zero.

查看:155
本文介绍了长度不能小于零。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用silverlight中的列表框和图像控件5.尝试通过在列表框中选择图像名称来显示图像控件中的图像。下面是listbox_selection中使用的代码更改事件。这个例外的原因可能是什么。



代码:

Hello everyone,

I am working with listbox and image controls in silverlight 5. Trying to display a image in image control by selecting a image name in the listbox. Below is the code using in listbox_selection changed event. What could be the reason for this exception.

Code:

ImgesList.Source = 
new BitmapImage(new Uri(App.Current.Host.Source.OriginalString.Substring(0, App.Current.Host.Source.OriginalString.IndexOf("ClientBin")) + "Images/" + listBox1.SelectedItem));





例外:



长度不能小于零。参数名称:长度



提前谢谢。



Exception:

Length cannot be less than zero. Parameter name: length

Thanks in advance.

推荐答案

在代码之前添加以下if语句:

Add the following if statement before your code:
if(listbox1.SelectedIndex >= 0)
... // your normal code here


如果方法Substring中的字符串长度为负,则会出现此错误。

您可以像这样捕获此异常:



This error occurs if the lenght of string in the method "Substring" is negative.
You can catch this exception like this:

try {
   string str = App.Current.Host.Source.OriginalString.Substring(0, App.Current.Host.Source.OriginalString.IndexOf("ClientBin");
}
catch (ArgumentOutOfRangeException e) {
   
}





这意味着,检查你的字符串是否有ClientBin:





That means, check whether your string have "ClientBin" at all:

int n = App.Current.Host.Source.OriginalString.IndexOf("ClientBin");
if (n > -1)
 ...



或类似的东西;)


or something like this ;)


这篇关于长度不能小于零。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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