Skip to content

Commit b2ab38e

Browse files
[C] avoid NRE on null GesturRecognizer (#25029)
- fixes #24900 Co-authored-by: Stephane Delcroix <[email protected]>
1 parent 0fce6c7 commit b2ab38e

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

src/Controls/src/Core/View/View.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ void AddItems(IList newItems)
101101
foreach (IElementDefinition item in newItems)
102102
{
103103
var gestureRecognizer = item as IGestureRecognizer;
104-
ValidateGesture(gestureRecognizer);
105-
item.Parent = this;
106-
GestureController.CompositeGestureRecognizers.Add(gestureRecognizer);
104+
if (ValidateGesture(gestureRecognizer))
105+
{
106+
item.Parent = this;
107+
GestureController.CompositeGestureRecognizers.Add(gestureRecognizer);
108+
}
107109
}
108110
}
109111

@@ -266,12 +268,13 @@ static void MarginPropertyChanged(BindableObject bindable, object oldValue, obje
266268
((View)bindable).InvalidateMeasureInternal(InvalidationTrigger.MarginChanged);
267269
}
268270

269-
void ValidateGesture(IGestureRecognizer gesture)
271+
bool ValidateGesture(IGestureRecognizer gesture)
270272
{
271273
if (gesture == null)
272-
return;
274+
return false;
273275
if (gesture is PinchGestureRecognizer && _gestureRecognizers.GetGesturesFor<PinchGestureRecognizer>().Count() > 1)
274276
throw new InvalidOperationException($"Only one {nameof(PinchGestureRecognizer)} per view is allowed");
277+
return true;
275278
}
276279

277280
#nullable enable
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
5+
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui24900">
6+
<Border>
7+
<Border.GestureRecognizers>
8+
<OnPlatform x:TypeArguments="GestureRecognizer">
9+
<OnPlatform.Platforms>
10+
<On Platform="Android">
11+
<TapGestureRecognizer Tapped="Header_Tapped" />
12+
</On>
13+
<!-- <On Platform="WinUI">
14+
<TapGestureRecognizer Tapped="Header_Tapped" />
15+
</On> -->
16+
</OnPlatform.Platforms>
17+
</OnPlatform>
18+
</Border.GestureRecognizers>
19+
</Border>
20+
</ContentPage>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Globalization;
5+
using System.Linq;
6+
using Microsoft.Maui.ApplicationModel;
7+
using Microsoft.Maui.Controls.Core.UnitTests;
8+
using Microsoft.Maui.Controls.Shapes;
9+
using Microsoft.Maui.Devices;
10+
using Microsoft.Maui.Dispatching;
11+
12+
using Microsoft.Maui.Graphics;
13+
using Microsoft.Maui.UnitTests;
14+
using NUnit.Framework;
15+
16+
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
17+
18+
public partial class Maui24900 : ContentPage
19+
{
20+
public Maui24900()
21+
{
22+
InitializeComponent();
23+
}
24+
25+
public Maui24900(bool useCompiledXaml)
26+
{
27+
//this stub will be replaced at compile time
28+
}
29+
30+
void Header_Tapped(object sender, TappedEventArgs e)
31+
{
32+
33+
}
34+
35+
[TestFixture]
36+
class Test
37+
{
38+
MockDeviceInfo mockDeviceInfo;
39+
40+
[SetUp]
41+
public void Setup()
42+
{
43+
Application.SetCurrentApplication(new MockApplication());
44+
DeviceInfo.SetCurrent(mockDeviceInfo = new MockDeviceInfo());
45+
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
46+
}
47+
48+
49+
[TearDown] public void TearDown()
50+
{
51+
AppInfo.SetCurrent(null);
52+
DeviceInfo.SetCurrent(null);
53+
}
54+
55+
[Test]
56+
public void OnPlatformDownNotThrow([Values(false, true)] bool useCompiledXaml)
57+
{
58+
mockDeviceInfo.Platform = DevicePlatform.WinUI;
59+
Assert.DoesNotThrow(()=> new Maui24900(useCompiledXaml));
60+
61+
62+
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)