我正在尝试编写一个例程来检查应用程序包中是否存在文件。在阅读了很多关于该主题的内容后,很明显 MS 忘记在 API 中放置一个 FileExists 函数(无论是否有意),但这是我目前所处的位置......
public async Task<bool> CheckFile(string filePath)
{
bool found = false;
try
{
Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
StorageFile file = await installedLocation.GetFileAsync("Assets\\" + filePath);
found = true;
}
catch (System.IO.FileNotFoundException ex)
{
found = false;
}
return found;
}
然后从以下位置调用:
private ImageSource _image = null;
private String _imagePath = null;
public ImageSource Image
{
get
{
if (this._image == null && this._imagePath != null)
{
Task<bool> fileExists = CheckFile(this._imagePath);
bool filefound = fileExists.Result;
string realPath = string.Empty;
if (filefound)
{
Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
realPath = installedLocation + "Assets\\" + this._imagePath;
}
else
{
realPath = "http://<<my url>>/images/" + this._imagePath;
}
this._image = new BitmapImage(new Uri(realPath));
}
return this._image;
}
set
{
this._imagePath = null;
this.SetProperty(ref this._image, value);
}
}
所以基本上它是检查本地是否存在图像,如果不存在则从我的网站获取它。
对于第一张图片来说,这一切似乎都很好,但是当它“返回 this._image;”时 对于第二张图片,一切都冻结了......
我只是不确定这里到底发生了什么..
有什么帮助吗?
干杯院长