Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 11dd05b

Browse files
Enable Nullable on XamarinCommunityToolkit (#1023)
* Enable Nullable on Unit Tests * Enable Nullable on Xamarin.CommunityToolkit.Markup * Enable Nullable on Xamarin.CommunityToolkit.Sample * Enable Nullable on Android, GTK, iOS and Tizen Samples * Enable Nullable for UWP & WPF Sample Projects * Add Nullability * Resolve Possible Null References * Removed Possible Null References * Update AppResources.Designer.cs * Handle Nullability * Updated Nullabiltiy * Update Converters & Unit Tests * Resolve MediaSource Unit Tests
1 parent 568ef2e commit 11dd05b

File tree

96 files changed

+1136
-968
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+1136
-968
lines changed

samples/XCT.Sample/Pages/Base/BasePage.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using System.Windows.Input;
34
using Xamarin.CommunityToolkit.ObjectModel;
45
using Xamarin.CommunityToolkit.Sample.Models;
@@ -9,7 +10,13 @@ namespace Xamarin.CommunityToolkit.Sample.Pages
910
public class BasePage : ContentPage
1011
{
1112
public BasePage() =>
12-
NavigateCommand = CommandFactory.Create<SectionModel>(sectionModel => Navigation.PushAsync(PreparePage(sectionModel)));
13+
NavigateCommand = CommandFactory.Create<SectionModel>(sectionModel =>
14+
{
15+
if (sectionModel != null)
16+
return Navigation.PushAsync(PreparePage(sectionModel));
17+
18+
return Task.CompletedTask;
19+
});
1320

1421
public Color DetailColor { get; set; }
1522

samples/XCT.Sample/Pages/TestCases/MediaElementSourcePage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ class MediaElementViewModel : BindableObject
1212
{
1313
public string VideoAsString { get; set; } = "https://tipcalculator.appwithkiss.com/video/Hint_1_2_EN_12.mov";
1414

15-
public MediaSource VideoAsMediaSource => MediaSource.FromUri(VideoAsString);
15+
public MediaSource? VideoAsMediaSource => MediaSource.FromUri(VideoAsString);
1616
}
1717
}

samples/XCT.Sample/Resx/AppResources.Designer.cs

Lines changed: 17 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/XCT.Sample/ViewModels/Converters/ItemSelectedEventArgsViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ public class ItemSelectedEventArgsViewModel
1616
};
1717

1818
public ICommand ItemSelectedCommand { get; } =
19-
CommandFactory.Create<Person>(person => Application.Current.MainPage.DisplayAlert("Item Tapped: ", person.Name, "Cancelf"));
19+
CommandFactory.Create<Person>(person => Application.Current.MainPage.DisplayAlert("Item Tapped: ", person?.Name, "Cancel"));
2020
}
2121
}

samples/XCT.Sample/ViewModels/Converters/ItemTappedEventArgsViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ItemTappedEventArgsViewModel
1515
};
1616

1717
public ICommand ItemTappedCommand { get; } =
18-
CommandFactory.Create<Person>(person => Application.Current.MainPage.DisplayAlert("Item Tapped: ", person.Name, "Cancel"));
18+
CommandFactory.Create<Person>(person => Application.Current.MainPage.DisplayAlert("Item Tapped: ", person?.Name, "Cancel"));
1919
}
2020

2121
public class Person

samples/XCT.Sample/ViewModels/Views/PopupControlViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async void OnDisplayPopup(Type popupType)
3535
{
3636
var view = (VisualElement)Activator.CreateInstance(popupType);
3737

38-
if (view is Popup<string> popup)
38+
if (view is Popup<string?> popup)
3939
{
4040
var result = await Navigation.ShowPopupAsync(popup);
4141
await Application.Current.MainPage.DisplayAlert("Popup Result", result, "OKAY");

src/CommunityToolkit/Xamarin.CommunityToolkit.UnitTests/Converters/ByteArrayToImageSourceConverter_Tests.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void ByteArrayToImageSourceConverter()
2222

2323
var result = byteArrayToImageSourceConverter.Convert(byteArray, typeof(ByteArrayToImageSourceConverter), null, CultureInfo.CurrentCulture);
2424

25-
Assert.True(StreamEquals(GetStreamFromImageSource((ImageSource)result), memoryStream));
25+
Assert.True(StreamEquals(GetStreamFromImageSource((ImageSource?)result), memoryStream));
2626
}
2727

2828
[Theory]
@@ -34,28 +34,32 @@ public void InvalidConverterValuesReturnsNull(object value)
3434
Assert.Throws<ArgumentException>(() => byteArrayToImageSourceConverter.Convert(value, typeof(ByteArrayToImageSourceConverter), null, CultureInfo.CurrentCulture));
3535
}
3636

37-
Stream GetStreamFromImageSource(ImageSource imageSource)
37+
Stream? GetStreamFromImageSource(ImageSource? imageSource)
3838
{
39-
var streamImageSource = (StreamImageSource)imageSource;
39+
var streamImageSource = (StreamImageSource?)imageSource;
4040

4141
var cancellationToken = System.Threading.CancellationToken.None;
42-
var task = streamImageSource.Stream(cancellationToken);
43-
return task.Result;
42+
var task = streamImageSource?.Stream(cancellationToken);
43+
return task?.Result;
4444
}
4545

46-
bool StreamEquals(Stream a, Stream b)
46+
bool StreamEquals(Stream? a, Stream? b)
4747
{
4848
if (a == b)
4949
return true;
5050

51-
if (a == null ||
52-
b == null ||
53-
a.Length != b.Length)
51+
if (a == null
52+
|| b == null
53+
|| a.Length != b.Length)
54+
{
5455
return false;
56+
}
5557

5658
for (var i = 0; i < a.Length; i++)
59+
{
5760
if (a.ReadByte() != b.ReadByte())
5861
return false;
62+
}
5963

6064
return true;
6165
}

src/CommunityToolkit/Xamarin.CommunityToolkit.UnitTests/Helpers/WeakEventManagerTests/DelegateWeakEventManager_Delegate_Tests.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ void HandleDelegateTest(object sender, PropertyChangedEventArgs e)
5858
}
5959

6060
// Act
61+
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
6162
propertyChangedWeakEventManager.RaiseEvent(null, new PropertyChangedEventArgs("Test"), nameof(PropertyChanged));
63+
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
6264

6365
// Assert
6466
Assert.True(didEventFire);
@@ -173,7 +175,9 @@ public void WeakEventManagerDelegate_UnassignedEventManager()
173175
void HandleDelegateTest(object sender, PropertyChangedEventArgs e) => didEventFire = true;
174176

175177
// Act
178+
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
176179
unassignedEventManager.RaiseEvent(null, null, nameof(PropertyChanged));
180+
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
177181

178182
// Assert
179183
Assert.False(didEventFire);
@@ -205,9 +209,7 @@ public void WeakEventManagerDelegate_AddEventHandler_NullHandler()
205209
// Act
206210

207211
// Assert
208-
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference
209212
Assert.Throws<ArgumentNullException>(() => propertyChangedWeakEventManager.AddEventHandler(null));
210-
#pragma warning restore CS8625
211213
}
212214

213215
[Fact]
@@ -231,9 +233,7 @@ public void WeakEventManagerDelegate_AddEventHandler_EmptyEventName()
231233
// Act
232234

233235
// Assert
234-
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference
235236
Assert.Throws<ArgumentNullException>(() => propertyChangedWeakEventManager.AddEventHandler(null, string.Empty));
236-
#pragma warning restore CS8625
237237
}
238238

239239
[Fact]
@@ -244,9 +244,7 @@ public void WeakEventManagerDelegate_AddEventHandler_WhitespaceEventName()
244244
// Act
245245

246246
// Assert
247-
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference
248247
Assert.Throws<ArgumentNullException>(() => propertyChangedWeakEventManager.AddEventHandler(null, " "));
249-
#pragma warning restore CS8625
250248
}
251249

252250
[Fact]
@@ -257,9 +255,7 @@ public void WeakEventManagerDelegate_RemoveEventHandler_NullHandler()
257255
// Act
258256

259257
// Assert
260-
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference
261258
Assert.Throws<ArgumentNullException>(() => propertyChangedWeakEventManager.RemoveEventHandler(null));
262-
#pragma warning restore CS8625
263259
}
264260

265261
[Fact]
@@ -283,9 +279,7 @@ public void WeakEventManagerDelegate_RemoveEventHandler_EmptyEventName()
283279
// Act
284280

285281
// Assert
286-
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference
287282
Assert.Throws<ArgumentNullException>(() => propertyChangedWeakEventManager.RemoveEventHandler(null, string.Empty));
288-
#pragma warning restore CS8625
289283
}
290284

291285
[Fact]
@@ -296,9 +290,7 @@ public void WeakEventManagerDelegate_RemoveEventHandler_WhiteSpaceEventName()
296290
// Act
297291

298292
// Assert
299-
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference
300293
Assert.Throws<ArgumentNullException>(() => propertyChangedWeakEventManager.RemoveEventHandler(null, " "));
301-
#pragma warning restore CS8625
302294
}
303295
}
304296
}

src/CommunityToolkit/Xamarin.CommunityToolkit.UnitTests/Helpers/WeakEventManagerTests/DelegateWeakEventManager_EventHandler_Tests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ void HandleTestEvent(object? sender, EventArgs e)
5252
}
5353

5454
// Act
55+
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
5556
TestWeakEventManager.RaiseEvent(null, new EventArgs(), nameof(TestEvent));
57+
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
5658

5759
// Assert
5860
Assert.True(didEventFire);
@@ -145,7 +147,9 @@ public void WeakEventManager_UnassignedEvent()
145147
void HandleTestEvent(object? sender, EventArgs e) => didEventFire = true;
146148

147149
// Act
150+
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
148151
TestWeakEventManager.RaiseEvent(null, null, nameof(TestEvent));
152+
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
149153

150154
// Assert
151155
Assert.False(didEventFire);
@@ -162,7 +166,9 @@ public void WeakEventManager_UnassignedEventManager()
162166
void HandleTestEvent(object? sender, EventArgs e) => didEventFire = true;
163167

164168
// Act
169+
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
165170
unassignedEventManager.RaiseEvent(null, null, nameof(TestEvent));
171+
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
166172

167173
// Assert
168174
Assert.False(didEventFire);

src/CommunityToolkit/Xamarin.CommunityToolkit.UnitTests/Helpers/WeakEventManagerTests/WeakEventManager_ActionT_Tests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ public void WeakEventManagerActionT_AddEventHandler_NullHandler()
140140
// Act
141141

142142
// Assert
143+
#pragma warning disable CS8604 // Possible null reference argument.
143144
Assert.Throws<ArgumentNullException>(() => actionEventManager.AddEventHandler(nullAction, nameof(ActionEvent)));
145+
#pragma warning restore CS8604 // Possible null reference argument.
144146

145147
}
146148

@@ -152,7 +154,9 @@ public void WeakEventManagerActionT_AddEventHandler_NullEventName()
152154
// Act
153155

154156
// Assert
157+
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
155158
Assert.Throws<ArgumentNullException>(() => actionEventManager.AddEventHandler(s => { var temp = s; }, null));
159+
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
156160
}
157161

158162
[Fact]
@@ -164,7 +168,9 @@ public void WeakEventManagerActionT_AddEventHandler_EmptyEventName()
164168
// Act
165169

166170
// Assert
171+
#pragma warning disable CS8604 // Possible null reference argument.
167172
Assert.Throws<ArgumentNullException>(() => actionEventManager.AddEventHandler(nullAction, string.Empty));
173+
#pragma warning restore CS8604 // Possible null reference argument.
168174
}
169175

170176
[Fact]
@@ -187,7 +193,9 @@ public void WeakEventManagerActionT_RemoveEventHandler_NullHandler()
187193
// Act
188194

189195
// Assert
196+
#pragma warning disable CS8604 // Possible null reference argument.
190197
Assert.Throws<ArgumentNullException>(() => actionEventManager.RemoveEventHandler(nullAction));
198+
#pragma warning restore CS8604 // Possible null reference argument.
191199
}
192200

193201
[Fact]
@@ -198,7 +206,9 @@ public void WeakEventManagerActionT_RemoveEventHandler_NullEventName()
198206
// Act
199207

200208
// Assert
209+
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
201210
Assert.Throws<ArgumentNullException>(() => actionEventManager.RemoveEventHandler(s => { var temp = s; }, null));
211+
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
202212
}
203213

204214
[Fact]

0 commit comments

Comments
 (0)