C# 按他的名字在while循环中获取文件
我有一个while循环:C# 按他的名字在while循环中获取文件,c#,arrays,image,list,uri,C#,Arrays,Image,List,Uri,我有一个while循环: int a = 0; while (list_Level[a] < Initial_Lvl) { var dOpt = new DataGridObjectOpt(); dOpt.ImageSource = new Uri(filePaths[a], UriKind.RelativeOrAbsolute); a++; } 但我仍然希望它像以前一样在文件路径中搜索这些图像 谢谢。您必须使用双引号中定义的字符串初始化字符串数组,并且需要将
int a = 0;
while (list_Level[a] < Initial_Lvl)
{
var dOpt = new DataGridObjectOpt();
dOpt.ImageSource = new Uri(filePaths[a], UriKind.RelativeOrAbsolute);
a++;
}
但我仍然希望它像以前一样在文件路径中搜索这些图像
谢谢。您必须使用双引号中定义的字符串初始化字符串数组,并且需要将这些方括号替换为花括号:
string[] name = {"apple", "orange", "banana"};
您还需要将ImageSource
赋值中的分号替换为逗号:
dOpt.ImageSource = new Uri(string.Format("{0}.jpg", name[a]), UriKind.RelativeOrAbsolute);
最后,在while
语句中声明DataGridObjectOpt
对象,因此它们只在循环中存在,这意味着代码实际上没有做任何事情
当我们不知道你到底想做什么的时候,这也使得我们几乎不可能给你一个答案。您对这些dOpt
对象有什么计划
这是我给你的最佳答案。它从您定义的文件路径
和名称
列表中创建Uri
对象列表。然后,您可以稍后使用它来分配DataGridObjectOpt
对象ImageSource
属性:
var imageSourceUris = new List<Uri>();
for (int i = 0; i < Initial_Lvl; i++)
{
imageSourceUris.Add(new Uri(filePaths[i], UriKind.RelativeOrAbsolute));
}
string[] names = {"apple", "orange", "banana"};
foreach (var name in names)
{
imageSourceUris.Add(new Uri($"{name}.jpg", UriKind.RelativeOrAbsolute));
}
// Now you have a list of Uris that you can assign to
// different DataGridObjectOpt object ListSource properties
// This example just creates a bunch of them and assigns the property
var dataGridOptions = new List<DataGridObjectOpt>();
foreach (var imageSourceUri in imageSourceUris)
{
dataGridOptions.Add(new DataGridObjectOpt {ImageSource = imageSourceUri});
}
var-imageSourceUris=newlist();
对于(int i=0;i
什么?什么是冷却液温度?它似乎与您发布的其余代码没有任何关系。另外,您的第二个代码段存在一些问题,这些问题可能会阻止它进行编译。我不知道你想做什么。我有几个列表,最终我会将它们逐项源化到数据网格中。其中一个列表由图像组成,它将成为dataGrid中的一列。我只想把注意力集中在我问题中的这个专栏上,所以我把它贴了出来。
var imageSourceUris = new List<Uri>();
for (int i = 0; i < Initial_Lvl; i++)
{
imageSourceUris.Add(new Uri(filePaths[i], UriKind.RelativeOrAbsolute));
}
string[] names = {"apple", "orange", "banana"};
foreach (var name in names)
{
imageSourceUris.Add(new Uri($"{name}.jpg", UriKind.RelativeOrAbsolute));
}
// Now you have a list of Uris that you can assign to
// different DataGridObjectOpt object ListSource properties
// This example just creates a bunch of them and assigns the property
var dataGridOptions = new List<DataGridObjectOpt>();
foreach (var imageSourceUri in imageSourceUris)
{
dataGridOptions.Add(new DataGridObjectOpt {ImageSource = imageSourceUri});
}