chore: Try Catch

This commit is contained in:
tangge233
2024-10-09 00:24:47 +08:00
parent 71d915f3ef
commit 50112b70bd
+30 -26
View File
@@ -30,32 +30,36 @@ Public Class MyImage
Set(value As String) Set(value As String)
If String.IsNullOrEmpty(value) Then Exit Property If String.IsNullOrEmpty(value) Then Exit Property
_SourceData = value _SourceData = value
If Not value.StartsWithF("http") Then ' 本地资源直接使用 Try
MyBase.Source = New MyBitmap(_SourceData) If Not value.StartsWithF("http") Then ' 本地资源直接使用
Exit Property MyBase.Source = New MyBitmap(_SourceData)
End If Exit Property
If _DownloadTask IsNot Nothing AndAlso Not _DownloadTask.IsCompleted Then ' 之前下载任务还在,直接砍了 End If
_DownloadTask.Dispose() If _DownloadTask IsNot Nothing AndAlso Not _DownloadTask.IsCompleted Then ' 之前下载任务还在,直接砍了
End If _DownloadTask.Dispose()
Dim NeedDownload As Boolean = True '是否需要下载/本地是否有有效缓存 End If
Dim TempFilePath As String = PathTemp & "Cache\MyImage\" & GetHash(_SourceData) & ".png" Dim NeedDownload As Boolean = True '是否需要下载/本地是否有有效缓存
If _UseCache And File.Exists(TempFilePath) And (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下 Dim TempFilePath As String = PathTemp & "Cache\MyImage\" & GetHash(_SourceData) & ".png"
If Not NeedDownload Then If _UseCache And File.Exists(TempFilePath) And (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下
MyBase.Source = New MyBitmap(TempFilePath) If Not NeedDownload Then
Exit Property MyBase.Source = New MyBitmap(TempFilePath)
End If Exit Property
' 异步下载图片 End If
Dim TaskID = 0 ' 异步下载图片
_DownloadTask = New Task(Sub() Dim TaskID = 0
NetDownload(_SourceData, TempFilePath, True) _DownloadTask = New Task(Sub()
End Sub) NetDownload(_SourceData, TempFilePath, True)
TaskID = _DownloadTask.Id End Sub)
_DownloadTask.Start() TaskID = _DownloadTask.Id
_DownloadTask.ContinueWith(Sub(t) _DownloadTask.Start()
If t.IsCompleted AndAlso t.Id = TaskID Then ' 任务没有被干掉 _DownloadTask.ContinueWith(Sub(t)
MyBase.Source = New MyBitmap(TempFilePath) If t.IsCompleted AndAlso t.Id = TaskID Then ' 任务没有被干掉
End If MyBase.Source = New MyBitmap(TempFilePath)
End Sub, TaskScheduler.FromCurrentSynchronizationContext()) End If
End Sub, TaskScheduler.FromCurrentSynchronizationContext())
Catch ex As Exception
Log(ex, "加载图片失败")
End Try
End Set End Set
End Property End Property
Public Shared Shadows ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback( Public Shared Shadows ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback(