-
-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MyImage 控件 #4868
Merged
Merged
MyImage 控件 #4868
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9447638
feat: MyImage 控件
tangge233 805a0e1
fix: 测试忘记删了
tangge233 a6aa784
feat: 修改实现方式
tangge233 e782d0e
fix: apply suggestions
tangge233 788f7c9
fix: 不使用磁盘缓存
tangge233 e6c1b19
Revert MyBitmap.vb
tangge233 71d915f
feat: UseCache 选项和异步加载图片
tangge233 50112b7
chore: Try Catch
tangge233 494e4f6
Merge branch 'Hex-Dragon:main' into WebP
tangge233 7ba082c
chore: apply suggestions
tangge233 e1d5815
chore: add summary tip
tangge233 8d266c8
chore: apply suggestions
tangge233 6de59a7
Update MyImage.vb
LTCatt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
Imports System.Threading.Tasks | ||
|
||
Public Class MyImage | ||
Inherits Image | ||
|
||
'事件 | ||
|
||
Private _SourceData As String = "" | ||
Private _UseCache As Boolean = False | ||
|
||
Private FileCacheExpiredTime As TimeSpan = New TimeSpan(7, 0, 0, 0) ' 一个星期的缓存有效期 | ||
|
||
''' <summary> | ||
''' 是否使用缓存,需要先于 Source 属性设置,否则无效 | ||
''' </summary> | ||
''' <returns></returns> | ||
Public Property UseCache As Boolean | ||
Get | ||
Return _UseCache | ||
End Get | ||
Set(value As Boolean) | ||
_UseCache = value | ||
End Set | ||
End Property | ||
|
||
''' <summary> | ||
''' 重写Image的Source属性 | ||
''' </summary> | ||
Public Shadows Property Source As String | ||
Get | ||
Return _SourceData | ||
End Get | ||
Set(value As String) | ||
If String.IsNullOrEmpty(value) Then Exit Property | ||
_SourceData = value | ||
Try | ||
If Not value.StartsWithF("http") Then ' 本地资源直接使用 | ||
MyBase.Source = New MyBitmap(_SourceData) | ||
Exit Property | ||
End If | ||
Dim NeedDownload As Boolean = True '是否需要下载/本地是否有有效缓存 | ||
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 ' 缓存文件存在且未过期,不需要重下 | ||
tangge233 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
If Not NeedDownload Then | ||
MyBase.Source = New MyBitmap(TempFilePath) | ||
Exit Property | ||
End If | ||
' 开一个线程处理在线图片 | ||
RunInNewThread(Sub() PicLoader(_SourceData, TempFilePath), "MyImage PicLoader " & GetUuid() & "#", ThreadPriority.BelowNormal) | ||
|
||
Catch ex As Exception | ||
Log(ex, "加载图片失败") | ||
End Try | ||
End Set | ||
End Property | ||
Public Shared Shadows ReadOnly SourceProperty As DependencyProperty = DependencyProperty.Register("Source", GetType(String), GetType(MyImage), New PropertyMetadata(New PropertyChangedCallback( | ||
Sub(sender As DependencyObject, e As DependencyPropertyChangedEventArgs) | ||
If Not IsNothing(sender) Then | ||
If String.IsNullOrEmpty(e.NewValue.ToString()) Then Exit Sub | ||
CType(sender, MyImage).Source = e.NewValue.ToString() | ||
End If | ||
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) | ||
tangge233 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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] 下载图片失败") | ||
End If | ||
End Try | ||
End Sub | ||
|
||
End Class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,4 +152,4 @@ Public Class MyBitmap | |
End Using | ||
End Sub | ||
|
||
End Class | ||
End Class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要注册为 DependencyProperty。
默认值为 True。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emmmmm
我这出了点问题,不知道为什么设置还是需要顺序……
NEED HELP
@LTCatt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我改完了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emmmm 似乎是 WPF 的特性?
新改后的代码测试发现顺序还是会影响实际结果……
使用下面代码很显然是期望不触发缓存机制,但是却使用了缓存
将
EnableCache
属性移到前面才会正常工作……There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修了,等更新的时候再推送 :D