Skip to content

Commit 6f244af

Browse files
authored
fix(Image): Add width/height/url to OnLoad event (microsoft#757)
Adds expected parameters to event data for image OnLoad event. Fixes microsoft#754
1 parent 269e286 commit 6f244af

File tree

2 files changed

+74
-7
lines changed

2 files changed

+74
-7
lines changed

ReactWindows/ReactNative/Views/Image/ReactImageLoadEvent.cs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ReactNative.UIManager.Events;
1+
using Newtonsoft.Json.Linq;
2+
using ReactNative.UIManager.Events;
23
using System;
34
using static System.FormattableString;
45

@@ -25,16 +26,35 @@ public class ReactImageLoadEvent : Event
2526
public const int OnLoadStart = 3;
2627

2728
private readonly int _eventType;
29+
private readonly string _imageUri;
30+
private readonly int _width;
31+
private readonly int _height;
2832

2933
/// <summary>
3034
/// Instantiates a <see cref="ReactImageLoadEvent"/>.
3135
/// </summary>
3236
/// <param name="viewId">The view identifier.</param>
3337
/// <param name="eventType">The event identifier.</param>
34-
public ReactImageLoadEvent(int viewId, int eventType)
38+
public ReactImageLoadEvent(int viewId, int eventType)
39+
: this(viewId, eventType, null, 0, 0)
40+
{
41+
}
42+
43+
/// <summary>
44+
/// Instantiates a <see cref="ReactImageLoadEvent"/>.
45+
/// </summary>
46+
/// <param name="viewId">The view identifier.</param>
47+
/// <param name="eventType">The event identifier.</param>
48+
/// <param name="imageUri">The image URI.</param>
49+
/// <param name="width">The image width.</param>
50+
/// <param name="height">The image height.</param>
51+
public ReactImageLoadEvent(int viewId, int eventType, string imageUri, int width, int height)
3552
: base(viewId, TimeSpan.FromTicks(Environment.TickCount))
3653
{
3754
_eventType = eventType;
55+
_imageUri = imageUri;
56+
_width = width;
57+
_height = height;
3858
}
3959

4060
/// <summary>
@@ -76,7 +96,35 @@ public override short CoalescingKey
7696
/// <param name="eventEmitter">The event emitter.</param>
7797
public override void Dispatch(RCTEventEmitter eventEmitter)
7898
{
79-
eventEmitter.receiveEvent(ViewTag, EventName, null);
99+
var eventData = default(JObject);
100+
101+
if (_imageUri != null || _eventType == OnLoad)
102+
{
103+
eventData = new JObject();
104+
105+
if (_imageUri != null)
106+
{
107+
eventData.Add("uri", _imageUri);
108+
}
109+
110+
if (_eventType == OnLoad)
111+
{
112+
var sourceData = new JObject
113+
{
114+
{ "width", _width },
115+
{ "height", _height },
116+
};
117+
118+
if (_imageUri != null)
119+
{
120+
sourceData.Add("url", _imageUri);
121+
}
122+
123+
eventData.Add("source", sourceData);
124+
}
125+
}
126+
127+
eventEmitter.receiveEvent(ViewTag, EventName, eventData);
80128
}
81129
}
82130
}

ReactWindows/ReactNative/Views/Image/ReactImageManager.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,29 @@ private void OnImageStatusUpdate(Border view, ImageLoadStatus status)
259259
.GetNativeModule<UIManagerModule>()
260260
.EventDispatcher;
261261

262-
eventDispatcher.DispatchEvent(
263-
new ReactImageLoadEvent(
264-
view.GetTag(),
265-
(int)status));
262+
if (status == ImageLoadStatus.OnLoad)
263+
{
264+
var bitmapImage = GetBitmapImage(view);
265+
eventDispatcher.DispatchEvent(
266+
new ReactImageLoadEvent(
267+
view.GetTag(),
268+
(int)status,
269+
bitmapImage.UriSource.AbsolutePath,
270+
bitmapImage.PixelWidth,
271+
bitmapImage.PixelHeight));
272+
}
273+
else
274+
{
275+
eventDispatcher.DispatchEvent(
276+
new ReactImageLoadEvent(
277+
view.GetTag(),
278+
(int)status));
279+
}
280+
}
281+
282+
private BitmapImage GetBitmapImage(Border view)
283+
{
284+
return (BitmapImage)((ImageBrush)view.Background).ImageSource;
266285
}
267286

268287
/// <summary>

0 commit comments

Comments
 (0)