| 1 | using Core.Librarys.Image; |
| 2 | using System; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Diagnostics; |
| 5 | using System.Drawing; |
| 6 | using System.IO; |
| 7 | using System.Linq; |
| 8 | using System.Text; |
| 9 | using System.Threading; |
| 10 | using System.Threading.Tasks; |
| 11 | using System.Windows; |
| 12 | using System.Windows.Controls; |
| 13 | using System.Windows.Data; |
| 14 | using System.Windows.Documents; |
| 15 | using System.Windows.Input; |
| 16 | using System.Windows.Media; |
| 17 | using System.Windows.Media.Imaging; |
| 18 | using System.Windows.Navigation; |
| 19 | using System.Windows.Shapes; |
| 20 | using System.Windows.Threading; |
| 21 | |
| 22 | namespace UI.Controls.Base |
| 23 | { |
| 24 | public class Img : Control |
| 25 | { |
| 26 | public CornerRadius Radius |
| 27 | { |
| 28 | get { return (CornerRadius)GetValue(RadiusProperty); } |
| 29 | set { SetValue(RadiusProperty, value); } |
| 30 | } |
| 31 | public static readonly DependencyProperty RadiusProperty = |
| 32 | DependencyProperty.Register("Radius", |
| 33 | typeof(CornerRadius), |
| 34 | typeof(Img)); |
| 35 | |
| 36 | public string Src |
| 37 | { |
| 38 | get { return (string)GetValue(SrcProperty); } |
| 39 | set { SetValue(SrcProperty, value); } |
| 40 | } |
| 41 | public static readonly DependencyProperty SrcProperty = |
| 42 | DependencyProperty.Register("Src", |
| 43 | typeof(string), |
| 44 | typeof(Img), new PropertyMetadata("pack://application:,,,/Tai;component/Resources/Icons/defaultIcon.png", new PropertyChangedCallback(OnPropertyChanged))); |
| 45 | |
| 46 | |
| 47 | |
| 48 | ///<summary> |
| 49 | /// |
| 50 | ///</summary> |
| 51 | public string URL |
| 52 | { |
| 53 | get { return (string)GetValue(URLProperty); } |
| 54 | set { SetValue(URLProperty, value); } |
| 55 | } |
| 56 | public static readonly DependencyProperty URLProperty = |
| 57 | DependencyProperty.Register("URL", |
| 58 | typeof(string), |
| 59 | typeof(Img), new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged))); |
| 60 | |
| 61 | private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 62 | { |
| 63 | var control = (d as Img); |
| 64 | if (e.Property == URLProperty && e.OldValue != e.NewValue && e.NewValue != null) |
| 65 | { |
| 66 | control.Handle(e.NewValue.ToString()); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public Img() |
| 71 | { |
| 72 | DefaultStyleKey = typeof(Img); |
| 73 | } |
| 74 | |
| 75 | private void Handle(string path) |
| 76 | { |
| 77 | string defaultIconFile = "pack://application:,,,/Tai;component/Resources/Icons/defaultIcon.png"; |
| 78 | if (string.IsNullOrEmpty(path)) |
| 79 | { |
| 80 | Src = defaultIconFile; |
| 81 | return; |
| 82 | } |
| 83 | if (path.IndexOf("pack:") != -1) |
| 84 | { |
| 85 | Src = path; |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | string src = path.IndexOf(":") != -1 ? path : System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path); |
| 90 | Src = File.Exists(src) ? src : defaultIconFile; |
| 91 | } |
| 92 | |
| 93 | } |
| 94 | } |