【发布时间】:2021-04-27 09:39:05
【问题描述】:
我有一个类,稍后我将其作为 Listbox 上的 ItemsSource 进行挂钩。这个列表框有一个项目,它有一个图像绑定“{Binding LocalImage}”。问题是,当 Item 从 LocalImage 获取值时,它不应该返回 null 值。 void doesImageExistLocally 完美运行,并已在另一个项目上进行了测试。
从 Main void 单独调用它确实会正确输出图像的路径,因此它不是 void 的问题,而是类/列表框项获取值的问题。还需要注意字符串 Image 也被正确分配。 Console.WriteLine(Logic.doesImageExistLocally("imageurl"));
public class Test {
private string _LocalImage;
public string Image {get; set:}
public string LocalImage
{
get { return _LocalImage; }
set { _LocalImage = doesImageExistLocally(Image); }
}
}
编辑:
Test test = new Test();
test.Image = "URL";
doesImageExistLocally(Image); // output = Images/name-of-picture.jpg
Console.WriteLine(test.LocalImage); // output = null;
作废代码:
public static string doesImageExistLocally(string imageURL)
{
Uri uri = new Uri(imageURL);
string path = "Images/" + System.IO.Path.GetFileName(uri.LocalPath);
Console.WriteLine(path);
if (!File.Exists(path))
{
using (WebClient client = new WebClient())
{
client.DownloadFile(new Uri(imageURL), path);
}
}
return path;
}
【问题讨论】:
-
ImageExistLocally(Image) 返回什么?你怎么称呼你的阶级和你的财产。
-
@maytham-ɯɐɥʇʎɐɯ 我编辑了这个问题,这就是我制作课程并设置/获取值的方式。谢谢!
-
你去@maytham-ɯɐɥʇʎɐɯ
-
昨天在我的位置很晚才睡着,今天早上让你回答。
标签: c# class data-binding listbox