Skip to content

Commit 0a2dff3

Browse files
zholobovreseul
authored andcommitted
Fix crash when Visibility is changed to Collapsed on a view. (microsoft#1840)
1 parent de12d1b commit 0a2dff3

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

ReactWindows/ReactNative/Accessibility/AccessibilityHelper.cs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,14 @@ private static bool GetDirty(UIElement element)
156156
return EnsureElementAccessibilityContext(element).Dirty;
157157
}
158158

159-
private static void MarkElementDirty(UIElement element, bool skipIfNoPeer = false)
159+
private static void MarkElementDirty(UIElement element)
160160
{
161161
// Retrieve automation peer
162162
var peer = FrameworkElementAutomationPeer.FromElement(element);
163163

164164
if (peer == null)
165165
{
166-
if (skipIfNoPeer)
167-
{
168-
return;
169-
}
170-
171-
throw new InvalidOperationException("Element has no automation peer");
166+
return;
172167
}
173168

174169
#if PERF_LOG
@@ -224,7 +219,7 @@ public static void OnChildAdded(UIElement parent, DependencyObject child)
224219
SetCurrentlyHidingChildren(parent, ElementAccessibilityContext.HidingChildren.NotSure);
225220

226221
// Mark the parent dirty (or skip if there is no associated peer available)
227-
MarkElementDirty(parent, true);
222+
MarkElementDirty(parent);
228223
}
229224

230225
/// <summary>
@@ -238,16 +233,41 @@ public static void OnChildRemoved(UIElement parent)
238233
parent.UpdateLayout();
239234

240235
// Mark the parent dirty (or skip if there is no associated peer available)
241-
MarkElementDirty(parent, true);
236+
MarkElementDirty(parent);
242237
}
243238

244239
/// <summary>
245240
/// Marks accessibility data in UI tree for update after a relevant (to accessibility) property of that element changed.
246241
/// For example, it is used by <see cref="RichTextBlock"/> to notify when text or structure is changed inside the text block.
247242
/// </summary>
248243
/// <param name="uiElement">The <see cref="UIElement"/>.</param>
249-
public static void OnElementChanged(UIElement uiElement)
244+
/// <param name="dependencyProperty">The dependency property that changed. It's important to provide this parameter if possible.</param>
245+
public static void OnElementChanged(UIElement uiElement, DependencyProperty dependencyProperty = null)
250246
{
247+
if (ReferenceEquals(UIElement.VisibilityProperty, dependencyProperty))
248+
{
249+
// add/remove like
250+
var fe = uiElement as FrameworkElement;
251+
if (fe == null)
252+
{
253+
return;
254+
}
255+
var parent = fe.Parent as UIElement;
256+
if (parent == null)
257+
{
258+
return;
259+
}
260+
261+
if (uiElement.Visibility == Visibility.Visible)
262+
{
263+
OnChildAdded(parent, fe);
264+
}
265+
else
266+
{
267+
OnChildRemoved(parent);
268+
}
269+
return;
270+
}
251271
// Mark the element dirty
252272
MarkElementDirty(uiElement);
253273
}

ReactWindows/ReactNative/UIManager/BaseViewManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public void SetZIndex(TFrameworkElement view, int zIndex)
124124
public void SetDisplay(TFrameworkElement view, string display)
125125
{
126126
view.Visibility = display == "none" ? Visibility.Collapsed : Visibility.Visible;
127+
AccessibilityHelper.OnElementChanged(view, UIElement.VisibilityProperty);
127128
}
128129

129130
/// <summary>

0 commit comments

Comments
 (0)