@@ -1,28 +1,23 @@
|
|||||||
Imports System.Threading.Tasks
|
Public Class MyImage
|
||||||
|
|
||||||
Public Class MyImage
|
|
||||||
Inherits Image
|
Inherits Image
|
||||||
|
|
||||||
'事件
|
|
||||||
|
|
||||||
Private _SourceData As String = ""
|
|
||||||
Private _UseCache As Boolean = False
|
|
||||||
|
|
||||||
Private FileCacheExpiredTime As TimeSpan = New TimeSpan(7, 0, 0, 0) ' 一个星期的缓存有效期
|
Private FileCacheExpiredTime As TimeSpan = New TimeSpan(7, 0, 0, 0) ' 一个星期的缓存有效期
|
||||||
|
|
||||||
''' <summary>
|
''' <summary>
|
||||||
''' 是否使用缓存,需要先于 Source 属性设置,否则无效
|
''' 是否使用缓存
|
||||||
''' </summary>
|
''' </summary>
|
||||||
''' <returns></returns>
|
''' <returns></returns>
|
||||||
Public Property UseCache As Boolean
|
Public Property UseCache As Boolean
|
||||||
Get
|
Get
|
||||||
Return _UseCache
|
Return GetValue(UseCacheProperty)
|
||||||
End Get
|
End Get
|
||||||
Set(value As Boolean)
|
Set(value As Boolean)
|
||||||
_UseCache = value
|
SetValue(UseCacheProperty, value)
|
||||||
End Set
|
End Set
|
||||||
End Property
|
End Property
|
||||||
|
Public Shared ReadOnly UseCacheProperty As DependencyProperty = DependencyProperty.Register("UseCache", GetType(Boolean), GetType(MyImage), New PropertyMetadata(True))
|
||||||
|
|
||||||
|
Private _SourceData As String = ""
|
||||||
''' <summary>
|
''' <summary>
|
||||||
''' 重写Image的Source属性
|
''' 重写Image的Source属性
|
||||||
''' </summary>
|
''' </summary>
|
||||||
@@ -40,12 +35,17 @@ Public Class MyImage
|
|||||||
End If
|
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 AndAlso File.Exists(TempFilePath) AndAlso (DateTime.Now - File.GetCreationTime(TempFilePath)) < FileCacheExpiredTime Then NeedDownload = False ' 缓存文件存在且未过期,不需要重下
|
||||||
If Not NeedDownload Then
|
If Not NeedDownload Then
|
||||||
MyBase.Source = New MyBitmap(TempFilePath)
|
MyBase.Source = New MyBitmap(TempFilePath)
|
||||||
Exit Property
|
Exit Property
|
||||||
End If
|
End If
|
||||||
' 开一个线程处理在线图片
|
|
||||||
|
If File.Exists(TempFilePath) Then '先显示着旧图片,下载新图片
|
||||||
|
Rename(TempFilePath, TempFilePath & ".old")
|
||||||
|
MyBase.Source = New MyBitmap(TempFilePath & ".old")
|
||||||
|
End If
|
||||||
|
' 开一个线程下载在线图片
|
||||||
RunInNewThread(Sub() PicLoader(_SourceData, TempFilePath), "MyImage PicLoader " & GetUuid() & "#", ThreadPriority.BelowNormal)
|
RunInNewThread(Sub() PicLoader(_SourceData, TempFilePath), "MyImage PicLoader " & GetUuid() & "#", ThreadPriority.BelowNormal)
|
||||||
|
|
||||||
Catch ex As Exception
|
Catch ex As Exception
|
||||||
@@ -65,13 +65,9 @@ Public Class MyImage
|
|||||||
Dim Retried As Boolean = False
|
Dim Retried As Boolean = False
|
||||||
RetryStart:
|
RetryStart:
|
||||||
Try
|
Try
|
||||||
If File.Exists(TempFilePath) Then '先显示着旧图片,下载新图片
|
Dim UnCompleteFile As String = TempFilePath & ".dl" '加一个下载中的后缀,防止中途关闭程序下载中断但是没下完,从而导致第二次显示的是损坏的图片……
|
||||||
Rename(TempFilePath, TempFilePath & ".old")
|
NetDownload(FileUrl, UnCompleteFile)
|
||||||
RunInUi(Sub()
|
Rename(UnCompleteFile, TempFilePath)
|
||||||
MyBase.Source = New MyBitmap(TempFilePath & ".old")
|
|
||||||
End Sub)
|
|
||||||
End If
|
|
||||||
NetDownload(FileUrl, TempFilePath)
|
|
||||||
RunInUi(Sub()
|
RunInUi(Sub()
|
||||||
MyBase.Source = New MyBitmap(TempFilePath)
|
MyBase.Source = New MyBitmap(TempFilePath)
|
||||||
End Sub)
|
End Sub)
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
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