Enable nullability in all samples (#20379)

This commit is contained in:
Julien Lebosquain
2026-01-05 10:09:27 +01:00
committed by GitHub
parent cf5b12e700
commit 3e41310917
35 changed files with 63 additions and 76 deletions
@@ -2,7 +2,6 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>$(AvsCurrentTargetFramework)</TargetFramework>
<Nullable>enable</Nullable>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
@@ -5,7 +5,7 @@ namespace BindingDemo;
internal class GenericMarkupExtension<T> : MarkupExtension
{
public T Value { get; set; }
public T? Value { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
@@ -6,7 +6,7 @@ namespace BindingDemo.ViewModels
{
[Phone]
[MaxLength(10)]
public string PhoneNumber { get; set; }
public string? PhoneNumber { get; set; }
[Range(0, 9)]
public int LessThan10 { get; set; }
@@ -9,7 +9,7 @@ namespace BindingDemo.ViewModels
{
private int _maximum = 10;
private int _value;
private string _valueError;
private string? _valueError;
public IndeiErrorViewModel()
{
@@ -34,16 +34,16 @@ namespace BindingDemo.ViewModels
set { this.RaiseAndSetIfChanged(ref _value, value); }
}
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;
public IEnumerable GetErrors(string propertyName)
public IEnumerable GetErrors(string? propertyName)
{
switch (propertyName)
{
case nameof(Value):
return new[] { _valueError };
default:
return null;
return Array.Empty<string>();
}
}
@@ -16,10 +16,10 @@ namespace BindingDemo.ViewModels
{
private string _booleanString = "True";
private double _doubleValue = 5.0;
private string _stringValue = "Simple Binding";
private string? _stringValue = "Simple Binding";
private bool _booleanFlag = false;
private string _currentTime;
private NestedCommandViewModel _nested;
private string? _currentTime;
private NestedCommandViewModel? _nested;
public MainWindowViewModel()
{
@@ -74,7 +74,7 @@ namespace BindingDemo.ViewModels
set { this.RaiseAndSetIfChanged(ref _doubleValue, value); }
}
public string StringValue
public string? StringValue
{
get { return _stringValue; }
set { this.RaiseAndSetIfChanged(ref _stringValue, value); }
@@ -86,7 +86,7 @@ namespace BindingDemo.ViewModels
set { this.RaiseAndSetIfChanged(ref _booleanFlag, value); }
}
public string CurrentTime
public string? CurrentTime
{
get { return _currentTime; }
private set { this.RaiseAndSetIfChanged(ref _currentTime, value); }
@@ -99,7 +99,7 @@ namespace BindingDemo.ViewModels
public ExceptionErrorViewModel ExceptionDataValidation { get; } = new ExceptionErrorViewModel();
public IndeiErrorViewModel IndeiDataValidation { get; } = new IndeiErrorViewModel();
public NestedCommandViewModel NestedModel
public NestedCommandViewModel? NestedModel
{
get { return _nested; }
private set { this.RaiseAndSetIfChanged(ref _nested, value); }
@@ -119,16 +119,16 @@ namespace BindingDemo.ViewModels
// Nested class, jsut so we can test it in XAML
public class TestItem<T> : ViewModelBase
{
private T _value;
private string _detail;
private T? _value;
private string? _detail;
public T Value
public T? Value
{
get { return _value; }
set { this.RaiseAndSetIfChanged(ref this._value, value); }
}
public string Detail
public string? Detail
{
get { return _detail; }
set { this.RaiseAndSetIfChanged(ref this._detail, value); }
@@ -3,7 +3,6 @@
<TargetFramework>$(AvsCurrentAndroidTargetFramework)</TargetFramework>
<SupportedOSPlatformVersion>$(AvsMinSupportedAndroidVersion)</SupportedOSPlatformVersion>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ApplicationId>com.Avalonia.ControlCatalog</ApplicationId>
<ApplicationVersion>1</ApplicationVersion>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
@@ -9,7 +9,7 @@ namespace ControlCatalog.Desktop;
public class EmbedSampleGtk : INativeDemoControl
{
private Process _mplayer;
private Process? _mplayer;
public IPlatformHandle CreateControl(bool isSecond, IPlatformHandle parent, Func<IPlatformHandle> createDefault)
{
@@ -20,7 +20,7 @@ internal class MacHelper
internal class MacOSViewHandle : INativeControlHostDestroyableControlHandle
{
private NSView _view;
private NSView? _view;
public MacOSViewHandle(NSView view)
{
@@ -32,7 +32,7 @@ internal class MacOSViewHandle : INativeControlHostDestroyableControlHandle
public void Destroy()
{
_view.Dispose();
_view?.Dispose();
_view = null;
}
}
@@ -44,7 +44,7 @@ internal unsafe class WinApi
[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string lpModuleName);
public static extern IntPtr GetModuleHandle(string? lpModuleName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr CreateWindowEx(
+3 -3
View File
@@ -74,12 +74,12 @@ namespace ControlCatalog.Desktop
{
DispatcherTimer.RunOnce(async () =>
{
var window = ((IClassicDesktopStyleApplicationLifetime)Application.Current.ApplicationLifetime)
.MainWindow;
var window = ((IClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!)
.MainWindow!;
var tc = window.GetLogicalDescendants().OfType<TabControl>().First();
foreach (var page in tc.Items.Cast<TabItem>().ToList())
{
if (page.Header.ToString() == "DatePicker" || page.Header.ToString() == "TreeView")
if (page.Header?.ToString() is "DatePicker" or "TreeView")
continue;
Console.WriteLine("Selecting " + page.Header);
tc.SelectedItem = page;
@@ -2,7 +2,6 @@
<PropertyGroup>
<TargetFrameworks>$(AvsCurrentTargetFramework)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<IncludeAvaloniaGenerators>true</IncludeAvaloniaGenerators>
</PropertyGroup>
<ItemGroup>
+1
View File
@@ -6,6 +6,7 @@
<EnableNETAnalyzers>false</EnableNETAnalyzers>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<LangVersion>14.0</LangVersion>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn);CS8002</NoWarn> <!-- ignore signing warnings for samples -->
</PropertyGroup>
-1
View File
@@ -4,7 +4,6 @@
<OutputType>Exe</OutputType>
<TargetFramework>$(AvsCurrentTargetFramework)</TargetFramework>
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
<Nullable>enable</Nullable>
<UseD3DCompiler>true</UseD3DCompiler>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
@@ -2,7 +2,6 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>$(AvsCurrentTargetFramework)</TargetFramework>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn);AVP1012;AVLN3001</NoWarn>
<ApplicationManifest>app.manifest</ApplicationManifest>
<IncludeAvaloniaGenerators>true</IncludeAvaloniaGenerators>
-1
View File
@@ -2,7 +2,6 @@
<PropertyGroup>
<TargetFramework>$(AvsCurrentTargetFramework)</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
+4 -4
View File
@@ -12,12 +12,12 @@ namespace PlatformSanityChecks
{
public class Program
{
static Thread UiThread;
static Thread UiThread = null!;
static void Main(string[] args)
{
UiThread = Thread.CurrentThread;
AppBuilder.Configure<App>().RuntimePlatformServicesInitializer();
AppBuilder.Configure<App>().RuntimePlatformServicesInitializer!();
var app = new App();
AvaloniaX11PlatformExtensions.InitializeX11Platform();
@@ -41,13 +41,13 @@ namespace PlatformSanityChecks
throw new Exception(error);
}
static IDisposable Enter([CallerMemberName] string caller = null)
static IDisposable Enter([CallerMemberName] string? caller = null)
{
Console.WriteLine("Entering " + caller);
return Disposable.Create(() => { Console.WriteLine("Leaving " + caller); });
}
static void EnterLoop(Action<CancellationTokenSource> cb, [CallerMemberName] string caller = null)
static void EnterLoop(Action<CancellationTokenSource> cb, [CallerMemberName] string? caller = null)
{
using (Enter(caller))
{
+7 -7
View File
@@ -17,22 +17,22 @@ namespace Previewer
<TextBlock>Hello world!</TextBlock>
</Window>";
private IAvaloniaRemoteTransportConnection _connection;
private IAvaloniaRemoteTransportConnection? _connection;
private Control _errorsContainer;
private TextBlock _errors;
private RemoteWidget _remote;
private RemoteWidget? _remote;
public MainWindow()
{
this.InitializeComponent();
var tb = this.FindControl<TextBox>("Xaml");
var tb = this.GetControl<TextBox>("Xaml");
tb.Text = InitialXaml;
var scroll = this.FindControl<ScrollViewer>("Remote");
var scroll = this.GetControl<ScrollViewer>("Remote");
var rem = new Center();
scroll.Content = rem;
_errorsContainer = this.FindControl<Control>("ErrorsContainer");
_errors = this.FindControl<TextBlock>("Errors");
_errorsContainer = this.GetControl<Control>("ErrorsContainer");
_errors = this.GetControl<TextBlock>("Errors");
tb.GetObservable(TextBox.TextProperty).Subscribe(text => _connection?.Send(new UpdateXamlMessage
{
Xaml = text
@@ -70,7 +70,7 @@ namespace Previewer
_errorsContainer.IsVisible = result.Error != null;
_errors.Text = result.Error ?? "";
}
if (obj is RequestViewportResizeMessage resize)
if (obj is RequestViewportResizeMessage resize && _remote is not null)
{
_remote.Width = Math.Min(4096, Math.Max(resize.Width, 1));
_remote.Height = Math.Min(4096, Math.Max(resize.Height, 1));
+2 -2
View File
@@ -31,7 +31,7 @@ namespace RenderDemo.Pages
public CustomDrawOp(Rect bounds, GlyphRun noSkia)
{
_noSkia = noSkia.TryCreateImmutableGlyphRunReference();
_noSkia = noSkia.TryCreateImmutableGlyphRunReference()!;
Bounds = bounds;
}
@@ -42,7 +42,7 @@ namespace RenderDemo.Pages
public Rect Bounds { get; }
public bool HitTest(Point p) => false;
public bool Equals(ICustomDrawOperation other) => false;
public bool Equals(ICustomDrawOperation? other) => false;
static Stopwatch St = Stopwatch.StartNew();
public void Render(ImmediateDrawingContext context)
{
@@ -57,11 +57,11 @@ namespace RenderDemo.Pages
context.DrawText(formattedText, new Point(10, 0));
var geometry = formattedText.BuildGeometry(new Point(10 + formattedText.Width + 10, 0));
var geometry = formattedText.BuildGeometry(new Point(10 + formattedText.Width + 10, 0))!;
context.DrawGeometry(gradient, null, geometry);
var highlightGeometry = formattedText.BuildHighlightGeometry(new Point(10 + formattedText.Width + 10, 0));
var highlightGeometry = formattedText.BuildHighlightGeometry(new Point(10 + formattedText.Width + 10, 0))!;
context.DrawGeometry(null, new ImmutablePen(gradient.ToImmutable(), 2), highlightGeometry);
}
@@ -29,7 +29,7 @@ namespace RenderDemo.Pages
private float _fontSize = 20;
private int _direction = 10;
private DispatcherTimer _timer;
private DispatcherTimer? _timer;
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
@@ -48,7 +48,7 @@ namespace RenderDemo.Pages
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
_timer.Stop();
_timer?.Stop();
_timer = null;
}
@@ -88,7 +88,7 @@ namespace RenderDemo.Pages
private float _fontSize = 20;
private int _direction = 10;
private DispatcherTimer _timer;
private DispatcherTimer? _timer;
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
@@ -107,7 +107,7 @@ namespace RenderDemo.Pages
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
_timer.Stop();
_timer?.Stop();
_timer = null;
}
@@ -14,7 +14,7 @@ namespace RenderDemo.Pages
AffectsRender<PathMeasurementPage>(BoundsProperty);
}
private RenderTargetBitmap _bitmap;
private RenderTargetBitmap? _bitmap;
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{
@@ -24,7 +24,7 @@ namespace RenderDemo.Pages
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
{
_bitmap.Dispose();
_bitmap?.Dispose();
_bitmap = null;
base.OnDetachedFromLogicalTree(e);
}
@@ -37,6 +37,9 @@ namespace RenderDemo.Pages
public override void Render(DrawingContext context)
{
if (_bitmap is null)
return;
using (var bitmapCtx = _bitmap.CreateDrawingContext())
{
var basePath = new PathGeometry();
@@ -10,7 +10,7 @@ namespace RenderDemo.Pages
{
public class RenderTargetBitmapPage : Control
{
private RenderTargetBitmap _bitmap;
private RenderTargetBitmap? _bitmap;
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{
@@ -20,7 +20,7 @@ namespace RenderDemo.Pages
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
{
_bitmap.Dispose();
_bitmap?.Dispose();
_bitmap = null;
base.OnDetachedFromLogicalTree(e);
}
@@ -28,6 +28,9 @@ namespace RenderDemo.Pages
readonly Stopwatch _st = Stopwatch.StartNew();
public override void Render(DrawingContext context)
{
if (_bitmap is null)
return;
using (var ctx = _bitmap.CreateDrawingContext())
using (ctx.PushTransform(Matrix.CreateTranslation(-100, -100)
* Matrix.CreateRotation(_st.Elapsed.TotalSeconds)
@@ -9,7 +9,7 @@ namespace RenderDemo.Pages
{
public class TextFormatterPage : UserControl
{
private TextLine _textLine;
private TextLine? _textLine;
public TextFormatterPage()
{
@@ -50,7 +50,7 @@ namespace RenderDemo.Pages
{
var currentX = 0d;
foreach (var textRun in _textLine.TextRuns)
foreach (var textRun in _textLine?.TextRuns ?? [])
{
if (textRun is ControlRun controlRun)
{
@@ -78,7 +78,7 @@ namespace RenderDemo.Pages
_defaultProperties = defaultProperties;
}
public TextRun GetTextRun(int textSourceIndex)
public TextRun? GetTextRun(int textSourceIndex)
{
if (textSourceIndex >= _text.Length * 2 + TextRun.DefaultTextSourceLength)
{
@@ -13,8 +13,8 @@ namespace RenderDemo.Pages
{
public class WriteableBitmapPage : Control
{
private WriteableBitmap _unpremulBitmap;
private WriteableBitmap _premulBitmap;
private WriteableBitmap? _unpremulBitmap;
private WriteableBitmap? _premulBitmap;
private readonly Stopwatch _st = Stopwatch.StartNew();
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
@@ -38,6 +38,9 @@ namespace RenderDemo.Pages
public override void Render(DrawingContext context)
{
if (_unpremulBitmap is null || _premulBitmap is null)
return;
void FillPixels(WriteableBitmap bitmap, byte fillAlpha, bool premul)
{
using (var fb = bitmap.Lock())
@@ -3,7 +3,6 @@
<OutputType>Exe</OutputType>
<TargetFramework>$(AvsCurrentAndroidTargetFramework)</TargetFramework>
<SupportedOSPlatformVersion>$(AvsMinSupportedAndroidVersion)</SupportedOSPlatformVersion>
<Nullable>enable</Nullable>
<ApplicationId>com.avalonia.safeareademo</ApplicationId>
<ApplicationVersion>1</ApplicationVersion>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
@@ -2,7 +2,6 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>$(AvsCurrentTargetFramework)</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
@@ -4,7 +4,6 @@
<TargetFramework>$(AvsCurrentIOSTargetFramework)</TargetFramework>
<SupportedOSPlatformVersion>$(AvsMinSupportedIOSVersion)</SupportedOSPlatformVersion>
<ProvisioningType>manual</ProvisioningType>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
-1
View File
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(AvsCurrentTargetFramework)</TargetFramework>
<Nullable>enable</Nullable>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
</PropertyGroup>
@@ -2,7 +2,6 @@
<PropertyGroup>
<TargetFramework>$(AvsCurrentTargetFramework)</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
@@ -5,7 +5,6 @@
<TargetFrameworks Condition="'$(IncludeMacTargetSamples)' == 'true'">$(TargetFrameworks);$(AvsCurrentMacOSTargetFramework)</TargetFrameworks>
<OutputType>Exe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<IncludeAvaloniaGenerators>true</IncludeAvaloniaGenerators>
</PropertyGroup>
-1
View File
@@ -6,7 +6,6 @@
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
<ApplicationManifest>app.manifest</ApplicationManifest>
<IncludeAvaloniaGenerators>true</IncludeAvaloniaGenerators>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
@@ -3,7 +3,6 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>$(AvsCurrentTargetFramework)</TargetFramework>
<Nullable>enable</Nullable>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
@@ -2,7 +2,6 @@
<PropertyGroup>
<TargetFramework>$(AvsCurrentTargetFramework)</TargetFramework>
<Nullable>enable</Nullable>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
-8
View File
@@ -4,14 +4,6 @@
<OutputType>Exe</OutputType>
<TargetFramework>$(AvsCurrentTargetFramework)</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>$(AvsCurrentTargetFramework)</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
@@ -26,7 +26,7 @@ namespace Avalonia
/// <returns></returns>
public static int StartWithHeadlessVncPlatform(
this AppBuilder builder,
string host, int port,
string? host, int port,
string[] args,
ShutdownMode shutdownMode = ShutdownMode.OnLastWindowClose)
{
@@ -46,7 +46,7 @@ namespace Avalonia
/// <exception cref="InvalidOperationException"></exception>
public static int StartWithHeadlessVncPlatform(
this AppBuilder builder,
string host, int port,
string? host, int port,
string? password,
string[] args,
ShutdownMode shutdownMode = ShutdownMode.OnLastWindowClose)