This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageBrushEx.cs
244 lines (206 loc) · 8.38 KB
/
ImageBrushEx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using SoftwareKobo.Controls.Extensions;
using Windows.ApplicationModel;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
namespace SoftwareKobo.Controls
{
public class ImageBrushEx : XamlCompositionBrushBase
{
public static readonly DependencyProperty AlignmentXProperty = DependencyProperty.Register(nameof(AlignmentX), typeof(AlignmentX), typeof(ImageBrushEx), new PropertyMetadata(AlignmentX.Center, OnAlignmentXChanged));
public static readonly DependencyProperty AlignmentYProperty = DependencyProperty.Register(nameof(AlignmentY), typeof(AlignmentY), typeof(ImageBrushEx), new PropertyMetadata(AlignmentY.Center, OnAlignmentYChanged));
public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register(nameof(ImageSource), typeof(string), typeof(ImageBrushEx), new PropertyMetadata(default(string), OnImageSourceChanged));
public static readonly DependencyProperty StretchProperty = DependencyProperty.Register(nameof(Stretch), typeof(Stretch), typeof(ImageBrushEx), new PropertyMetadata(Stretch.Uniform, OnStretchChanged));
public event ImageFailedEventHandler ImageFailed;
public event EventHandler ImageOpened;
public AlignmentX AlignmentX
{
get
{
return (AlignmentX)GetValue(AlignmentXProperty);
}
set
{
SetValue(AlignmentXProperty, value);
}
}
public AlignmentY AlignmentY
{
get
{
return (AlignmentY)GetValue(AlignmentYProperty);
}
set
{
SetValue(AlignmentYProperty, value);
}
}
public string ImageSource
{
get
{
return (string)GetValue(ImageSourceProperty);
}
set
{
SetValue(ImageSourceProperty, value);
}
}
public Stretch Stretch
{
get
{
return (Stretch)GetValue(StretchProperty);
}
set
{
SetValue(StretchProperty, value);
}
}
protected override void OnConnected()
{
base.OnConnected();
SetImageSource(ImageSource);
}
protected override void OnDisconnected()
{
base.OnDisconnected();
DisposeCompositionBrush();
}
private static float ConvertAlignmentXToHorizontalAlignmentRatio(AlignmentX alignmentX)
{
switch (alignmentX)
{
case AlignmentX.Left:
return 0f;
case AlignmentX.Center:
return 0.5f;
case AlignmentX.Right:
return 1f;
default:
throw new ArgumentOutOfRangeException(nameof(alignmentX));
}
}
private static float ConvertAlignmentYToVerticalAlignmentRatio(AlignmentY alignmentY)
{
switch (alignmentY)
{
case AlignmentY.Top:
return 0f;
case AlignmentY.Center:
return 0.5f;
case AlignmentY.Bottom:
return 1f;
default:
throw new ArgumentOutOfRangeException(nameof(alignmentY));
}
}
private static void OnAlignmentXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var obj = (ImageBrushEx)d;
var value = (AlignmentX)e.NewValue;
if (!Enum.IsDefined(typeof(AlignmentX), value))
{
throw new ArgumentOutOfRangeException(nameof(AlignmentX));
}
var brush = (CompositionSurfaceBrush)obj.CompositionBrush;
brush.HorizontalAlignmentRatio = ConvertAlignmentXToHorizontalAlignmentRatio(value);
}
private static void OnAlignmentYChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var obj = (ImageBrushEx)d;
var value = (AlignmentY)e.NewValue;
if (!Enum.IsDefined(typeof(AlignmentY), value))
{
throw new ArgumentOutOfRangeException(nameof(AlignmentY));
}
var brush = (CompositionSurfaceBrush)obj.CompositionBrush;
brush.VerticalAlignmentRatio = ConvertAlignmentYToVerticalAlignmentRatio(value);
}
private static void OnImageSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var obj = (ImageBrushEx)d;
var value = (string)e.NewValue;
obj.SetImageSource(value);
}
private static void OnStretchChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var obj = (ImageBrushEx)d;
var value = (Stretch)e.NewValue;
if (!Enum.IsDefined(typeof(Stretch), value))
{
throw new ArgumentOutOfRangeException(nameof(Stretch));
}
var brush = (CompositionSurfaceBrush)obj.CompositionBrush;
brush.Stretch = (CompositionStretch)value;
}
private void DisposeCompositionBrush()
{
if (CompositionBrush != null)
{
CompositionBrush.Dispose();
CompositionBrush = null;
}
}
private async void SetImageSource(string imageSource)
{
var compositor = Window.Current.Compositor;
// 设计模式下直接显示。
if (DesignMode.DesignModeEnabled)
{
if (imageSource == null)
{
DisposeCompositionBrush();
}
else
{
var imageSurface = LoadedImageSurface.StartLoadFromUri(new Uri(imageSource, UriKind.RelativeOrAbsolute));
var args = await imageSurface.WaitForLoadCompletedAsync();
DisposeCompositionBrush();
if (args.Status == LoadedImageSourceLoadStatus.Success)
{
var brush = compositor.CreateSurfaceBrush(imageSurface);
brush.Stretch = (CompositionStretch)Stretch;
CompositionBrush = brush;
}
}
}
else
{
if (imageSource == null)
{
CompositionBrush = null;
}
else
{
var loader = ImageExSettings.Loader();
var result = await loader.GetImageSurfaceAsync(imageSource);
if (imageSource == ImageSource)// 确保在执行异步操作过程中,ImageSource 没有变动。
{
switch (result.Status)
{
case LoadedImageSourceLoadStatus.Success:
DisposeCompositionBrush();
var brush = compositor.CreateSurfaceBrush(result.Value);
brush.Stretch = (CompositionStretch)Stretch;
brush.HorizontalAlignmentRatio = ConvertAlignmentXToHorizontalAlignmentRatio(AlignmentX);
brush.VerticalAlignmentRatio = ConvertAlignmentYToVerticalAlignmentRatio(AlignmentY);
CompositionBrush = brush;
ImageOpened?.Invoke(this, EventArgs.Empty);
break;
case LoadedImageSourceLoadStatus.NetworkError:
case LoadedImageSourceLoadStatus.InvalidFormat:
case LoadedImageSourceLoadStatus.Other:
DisposeCompositionBrush();
ImageFailed?.Invoke(this, new ImageFailedEventArgs(imageSource, new ImageSurfaceFailedStatusException(result.Status)));
break;
default:
throw new ArgumentOutOfRangeException(nameof(result.Status));
}
}
}
}
}
}
}