개정판 6a19b48d
issue #00000 페이지 이미지 download 로직 수정
Change-Id: I4da03c36b5a135eea80b57b20ada38239afd1082
KCOM/Common/ImageAsyncHelper.cs | ||
---|---|---|
5 | 5 |
using System.Text; |
6 | 6 |
using System.Threading.Tasks; |
7 | 7 |
using System.Windows; |
8 |
using System.Windows.Controls; |
|
8 | 9 |
using System.Windows.Data; |
10 |
using System.Windows.Media.Imaging; |
|
9 | 11 |
|
10 | 12 |
namespace KCOM.Common |
11 | 13 |
{ |
12 |
public class ImageAsyncHelper : DependencyObject
|
|
14 |
public class LazyImage : DependencyObject
|
|
13 | 15 |
{ |
14 |
public static Uri GetSourceUri(DependencyObject obj) { return (Uri)obj.GetValue(SourceUriProperty); } |
|
15 |
public static void SetSourceUri(DependencyObject obj, Uri value) { obj.SetValue(SourceUriProperty, value); } |
|
16 |
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(string), typeof(LazyImage), new PropertyMetadata(null, OnSourcePropertyChanged)); |
|
16 | 17 |
|
17 |
public static readonly DependencyProperty SourceUriProperty = DependencyProperty.RegisterAttached("SourceUri", typeof(Uri), typeof(ImageAsyncHelper), new PropertyMetadata
|
|
18 |
private static async void OnSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
18 | 19 |
{ |
19 |
PropertyChangedCallback = (obj, e) => |
|
20 |
{ |
|
21 |
//if (((System.Windows.Controls.Image)obj).Source == null) |
|
22 |
//{ |
|
23 |
System.Diagnostics.Debug.WriteLine(e.NewValue); |
|
24 |
|
|
25 |
((System.Windows.Controls.Image)obj).SetBinding(System.Windows.Controls.Image.SourceProperty, |
|
26 |
new Binding("VerifiedUri") |
|
27 |
{ |
|
28 |
Source = new ImageAsyncHelper { GivenUri = (Uri)e.NewValue }, |
|
29 |
IsAsync = true, |
|
30 |
}); |
|
31 |
//} |
|
32 |
} |
|
33 |
}); |
|
20 |
Image imageControl = d as Image; |
|
21 |
string imagePath = e.NewValue as string; |
|
34 | 22 |
|
35 |
Uri GivenUri; |
|
36 |
public Uri VerifiedUri |
|
37 |
{ |
|
38 |
get |
|
23 |
if (imageControl != null && imagePath != null && !string.IsNullOrEmpty(imagePath)) |
|
39 | 24 |
{ |
40 |
try
|
|
25 |
if (imageControl.IsLoaded && imageControl.ActualWidth > 0 && imageControl.ActualHeight > 0)
|
|
41 | 26 |
{ |
42 |
//Dns.GetHostEntry(GivenUri.DnsSafeHost); |
|
43 |
return GivenUri; |
|
27 |
await LoadImageAsync(imagePath, imageControl); |
|
44 | 28 |
} |
45 |
catch (Exception)
|
|
29 |
else
|
|
46 | 30 |
{ |
47 |
return null; |
|
31 |
RoutedEventHandler onImageControlLoaded = null; |
|
32 |
onImageControlLoaded = async (sender, args) => |
|
33 |
{ |
|
34 |
imageControl.Loaded -= onImageControlLoaded; |
|
35 |
await LoadImageAsync(imagePath, imageControl); |
|
36 |
}; |
|
37 |
imageControl.Loaded += onImageControlLoaded; |
|
48 | 38 |
} |
49 |
|
|
50 | 39 |
} |
51 | 40 |
} |
41 |
|
|
42 |
public string Source |
|
43 |
{ |
|
44 |
get { return (string)GetValue(SourceProperty); } |
|
45 |
set { SetValue(SourceProperty, value); } |
|
46 |
} |
|
47 |
|
|
48 |
private static async Task LoadImageAsync(string path, Image imageControl) |
|
49 |
{ |
|
50 |
BitmapImage image = new BitmapImage(); |
|
51 |
image.BeginInit(); |
|
52 |
image.UriSource = new Uri(path); |
|
53 |
image.CacheOption = BitmapCacheOption.OnLoad; |
|
54 |
image.EndInit(); |
|
55 |
|
|
56 |
await image.Dispatcher.BeginInvoke(new Action(() => |
|
57 |
{ |
|
58 |
imageControl.Source = image; |
|
59 |
})); |
|
60 |
} |
|
52 | 61 |
} |
53 | 62 |
} |
내보내기 Unified diff