chore: apply suggestions
This commit is contained in:
@@ -7,7 +7,6 @@ Public Class MyImage
|
|||||||
|
|
||||||
Private _SourceData As String = ""
|
Private _SourceData As String = ""
|
||||||
Private _UseCache As Boolean = False
|
Private _UseCache As Boolean = False
|
||||||
Private _DownloadTask As Task
|
|
||||||
|
|
||||||
Private FileCacheExpiredTime As TimeSpan = New TimeSpan(7, 0, 0, 0) ' 一个星期的缓存有效期
|
Private FileCacheExpiredTime As TimeSpan = New TimeSpan(7, 0, 0, 0) ' 一个星期的缓存有效期
|
||||||
|
|
||||||
@@ -35,9 +34,6 @@ Public Class MyImage
|
|||||||
MyBase.Source = New MyBitmap(_SourceData)
|
MyBase.Source = New MyBitmap(_SourceData)
|
||||||
Exit Property
|
Exit Property
|
||||||
End If
|
End If
|
||||||
If _DownloadTask IsNot Nothing AndAlso Not _DownloadTask.IsCompleted Then ' 之前下载任务还在,直接砍了
|
|
||||||
_DownloadTask.Dispose()
|
|
||||||
End If
|
|
||||||
Dim NeedDownload As Boolean = True '是否需要下载/本地是否有有效缓存
|
Dim NeedDownload As Boolean = True '是否需要下载/本地是否有有效缓存
|
||||||
Dim TempFilePath As String = PathTemp & "Cache\MyImage\" & GetHash(_SourceData) & ".png"
|
Dim TempFilePath As String = PathTemp & "Cache\MyImage\" & GetHash(_SourceData) & ".png"
|
||||||
If _UseCache And File.Exists(TempFilePath) And (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下
|
If _UseCache And File.Exists(TempFilePath) And (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下
|
||||||
@@ -45,18 +41,9 @@ Public Class MyImage
|
|||||||
MyBase.Source = New MyBitmap(TempFilePath)
|
MyBase.Source = New MyBitmap(TempFilePath)
|
||||||
Exit Property
|
Exit Property
|
||||||
End If
|
End If
|
||||||
' 异步下载图片
|
' 开一个线程处理在线图片
|
||||||
Dim TaskID = 0
|
RunInNewThread(Sub() PicLoader(_SourceData, TempFilePath), "MyImage PicLoader " & GetUuid() & "#", ThreadPriority.BelowNormal)
|
||||||
_DownloadTask = New Task(Sub()
|
|
||||||
NetDownload(_SourceData, TempFilePath, True)
|
|
||||||
End Sub)
|
|
||||||
TaskID = _DownloadTask.Id
|
|
||||||
_DownloadTask.Start()
|
|
||||||
_DownloadTask.ContinueWith(Sub(t)
|
|
||||||
If t.IsCompleted AndAlso t.Id = TaskID Then ' 任务没有被干掉
|
|
||||||
MyBase.Source = New MyBitmap(TempFilePath)
|
|
||||||
End If
|
|
||||||
End Sub, TaskScheduler.FromCurrentSynchronizationContext())
|
|
||||||
Catch ex As Exception
|
Catch ex As Exception
|
||||||
Log(ex, "加载图片失败")
|
Log(ex, "加载图片失败")
|
||||||
End Try
|
End Try
|
||||||
@@ -70,4 +57,30 @@ Public Class MyImage
|
|||||||
End If
|
End If
|
||||||
End Sub)))
|
End Sub)))
|
||||||
|
|
||||||
|
Private Sub PicLoader(FileUrl As String, TempFilePath As String)
|
||||||
|
Dim Retried As Boolean = False
|
||||||
|
RetryStart:
|
||||||
|
Try
|
||||||
|
If File.Exists(TempFilePath) Then '先显示着旧图片,下载新图片
|
||||||
|
Rename(TempFilePath, TempFilePath & ".old")
|
||||||
|
RunInUi(Sub()
|
||||||
|
MyBase.Source = New MyBitmap(TempFilePath & ".old")
|
||||||
|
End Sub)
|
||||||
|
End If
|
||||||
|
NetDownload(FileUrl, TempFilePath)
|
||||||
|
RunInUi(Sub()
|
||||||
|
MyBase.Source = New MyBitmap(TempFilePath)
|
||||||
|
End Sub)
|
||||||
|
File.Delete(TempFilePath & ".old")
|
||||||
|
Catch ex As Exception
|
||||||
|
If Not Retried Then
|
||||||
|
Retried = True
|
||||||
|
GoTo RetryStart
|
||||||
|
Else
|
||||||
|
Log(ex, $"[MyImage] 下载图片失败")
|
||||||
|
RunInUi(Sub() MyBase.Source = New MyBitmap(PathImage & "Icons/NoIcon.png"))
|
||||||
|
End If
|
||||||
|
End Try
|
||||||
|
End Sub
|
||||||
|
|
||||||
End Class
|
End Class
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
xmlns:local="clr-namespace:PCL"> <!-- 不知道为啥只有这个文件不能在 XAML 设置 PanScroll -->
|
xmlns:local="clr-namespace:PCL"> <!-- 不知道为啥只有这个文件不能在 XAML 设置 PanScroll -->
|
||||||
<local:MyScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" x:Name="PanBack">
|
<local:MyScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" x:Name="PanBack">
|
||||||
<StackPanel Name="PanMain" Margin="25,25,25,10" Grid.IsSharedSizeScope="True">
|
<StackPanel Name="PanMain" Margin="25,25,25,10" Grid.IsSharedSizeScope="True">
|
||||||
|
<local:MyImage UseCache="True" Source="https://objects.lihaoyu.cn/bigcake-blog-images/2024/10/05/6700c8f7cd4b2.jpeg"/>
|
||||||
<StackPanel x:Name="PanCustom">
|
<StackPanel x:Name="PanCustom">
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
<local:MyCard Margin="0,0,0,15" Title="快照版提示" x:Name="PanHint">
|
<local:MyCard Margin="0,0,0,15" Title="快照版提示" x:Name="PanHint">
|
||||||
|
|||||||
Reference in New Issue
Block a user