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

Enable Nullability for Android, GTK, iOS, Tizen, UWP & WPF Sample Projects #1015

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion samples/XCT.Sample.Android/SplashActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Xamarin.CommunityToolkit.Sample.Droid
[Activity(Label = "XamarinCommunityToolkitSample", Icon = "@mipmap/icon", Theme = "@style/SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class SplashActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
var intent = new Intent(this, typeof(MainActivity));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>tizen40</TargetFramework>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<AppxPackageSigningEnabled>false</AppxPackageSigningEnabled>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM'">
<DebugSymbols>true</DebugSymbols>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<Deterministic>true</Deterministic>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down
7 changes: 5 additions & 2 deletions samples/XCT.Sample.iOS/Renderers/NoLineNavigationRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ public override void ViewWillAppear(bool animated)
// Newest iOS version fix - trycatch isn't optimal
try
{
NavigationBar.ScrollEdgeAppearance.ShadowImage = new UIKit.UIImage();
NavigationBar.ScrollEdgeAppearance.ShadowColor = null;
if (NavigationBar.ScrollEdgeAppearance != null)
{
NavigationBar.ScrollEdgeAppearance.ShadowImage = new UIKit.UIImage();
NavigationBar.ScrollEdgeAppearance.ShadowColor = null;
}
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<MtouchEnableSGenConc>true</MtouchEnableSGenConc>
<MtouchHttpClientHandler>NSUrlSessionHandler</MtouchHttpClientHandler>
<ProvisioningType>automatic</ProvisioningType>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhoneSimulator' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
84 changes: 38 additions & 46 deletions samples/XCT.Sample/Helpers/RelayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,21 @@ namespace Xamarin.CommunityToolkit.Sample
{
public class RelayCommand : ICommand
{
readonly Action execute;
readonly Func<Task> asyncExecute;
readonly Action? execute;
readonly Func<Task>? asyncExecute;
readonly Func<bool>? canExecute;

Func<bool> canExecute;
int executingCount;

public RelayCommand(Action execute, Func<bool> canExecute = null)
public RelayCommand(Action execute, Func<bool>? canExecute = null)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
this.execute = execute;
this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
this.canExecute = canExecute;
}

protected RelayCommand(Func<Task> execute, Func<bool> canExecute = null) // This ctor is protected here and public in a derived class, to allow simple initialization like new RelayCommand(MyMethod) without errors due to ambiguity
protected RelayCommand(Func<Task> execute, Func<bool>? canExecute = null) // This ctor is protected here and public in a derived class, to allow simple initialization like new RelayCommand(MyMethod) without errors due to ambiguity
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
asyncExecute = execute;
asyncExecute = execute ?? throw new ArgumentNullException(nameof(execute));
this.canExecute = canExecute;
}

Expand All @@ -34,7 +30,7 @@ public RelayCommand(Action execute, Func<bool> canExecute = null)
/// </summary>
/// <param name="parameter">Ignored; this is the paremeterless command class</param>
/// <returns></returns>
public bool CanExecute(object parameter = null)
public bool CanExecute(object? parameter = null)
{
try
{
Expand All @@ -47,15 +43,12 @@ public bool CanExecute(object parameter = null)
}
}

public event EventHandler CanExecuteChanged;
public event EventHandler? CanExecuteChanged;

public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);

// Asynchronous command handling based on http://stackoverflow.com/a/31595509/6043538
public async void Execute(object parameter = null)
public async void Execute(object? parameter = null)
{
var couldExecuteBeforeExecute = CanExecute();
if (!couldExecuteBeforeExecute)
Expand All @@ -70,8 +63,10 @@ public async void Execute(object parameter = null)
{
if (execute != null)
execute();
else
else if (asyncExecute != null)
await asyncExecute();
else
throw new Exception("Execute is null");
}
catch (Exception ex)
{
Expand All @@ -89,31 +84,30 @@ public async void Execute(object parameter = null)

public class RelayCommandAsync : RelayCommand
{
public RelayCommandAsync(Func<Task> execute, Func<bool> canExecute = null)
: base(execute, canExecute) { } // This ctor is public here and protected in the base class, to allow simple initialization like new RelayCommandAsync(MyMethod) without errors due to ambiguity
public RelayCommandAsync(Func<Task> execute, Func<bool>? canExecute = null)
: base(execute, canExecute)
{
// This ctor is public here and protected in the base class, to allow simple initialization like new RelayCommandAsync(MyMethod) without errors due to ambiguity
}
}

public class RelayCommand<TParameter> : ICommand
{
readonly Action<TParameter> execute;
readonly Func<TParameter, Task> asyncExecute;
readonly Action<TParameter>? execute;
readonly Func<TParameter, Task>? asyncExecute;
readonly Func<TParameter?, bool>? canExecute;

Func<TParameter, bool> canExecute;
int executingCount;

public RelayCommand(Action<TParameter> execute, Func<TParameter, bool> canExecute = null)
public RelayCommand(Action<TParameter> execute, Func<TParameter?, bool>? canExecute = null)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
this.execute = execute;
this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
this.canExecute = canExecute;
}

protected RelayCommand(Func<TParameter, Task> execute, Func<TParameter, bool> canExecute = null) // This ctor is protected here and public in a derived class, to allow simple initialization like new RelayCommand(MyMethod) without errors due to ambiguity
protected RelayCommand(Func<TParameter, Task> execute, Func<TParameter?, bool>? canExecute = null) // This ctor is protected here and public in a derived class, to allow simple initialization like new RelayCommand(MyMethod) without errors due to ambiguity
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
asyncExecute = execute;
asyncExecute = execute ?? throw new ArgumentNullException(nameof(execute));
this.canExecute = canExecute;
}

Expand All @@ -122,11 +116,11 @@ public RelayCommand(Action<TParameter> execute, Func<TParameter, bool> canExecut
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public bool CanExecute(object parameter = null)
public bool CanExecute(object? parameter = null)
{
try
{
return canExecute != null ? canExecute((TParameter)parameter) : executingCount == 0;
return canExecute != null ? canExecute((TParameter?)parameter) : executingCount == 0;
}
catch (Exception ex)
{
Expand All @@ -135,12 +129,9 @@ public bool CanExecute(object parameter = null)
}
}

public event EventHandler CanExecuteChanged;
public event EventHandler? CanExecuteChanged;

public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);

// Asynchronous command handling based on http://stackoverflow.com/a/31595509/6043538
public async void Execute(object parameterAsObject)
Expand All @@ -159,13 +150,11 @@ public async void Execute(object parameterAsObject)
var parameter = (TParameter)parameterAsObject;

if (execute != null)
{
execute(parameter);
}
else if (asyncExecute != null)
await asyncExecute.Invoke(parameter);
else
{
await asyncExecute(parameter);
}
throw new Exception("Execute is null");
}
catch (Exception ex)
{
Expand All @@ -183,7 +172,10 @@ public async void Execute(object parameterAsObject)

public class RelayCommandAsync<TParameter> : RelayCommand<TParameter>
{
public RelayCommandAsync(Func<TParameter, Task> execute, Func<TParameter, bool> canExecute = null)
: base(execute, canExecute) { } // This ctor is public here and protected in the base class, to allow simple initialization like new RelayCommandAsync(MyMethod) without errors due to ambiguity
public RelayCommandAsync(Func<TParameter, Task> execute, Func<TParameter?, bool>? canExecute = null)
: base(execute, canExecute)
{
// This ctor is public here and protected in the base class, to allow simple initialization like new RelayCommandAsync(MyMethod) without errors due to ambiguity
}
}
}
26 changes: 13 additions & 13 deletions samples/XCT.Sample/Helpers/XLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Xamarin.CommunityToolkit.Sample
/// </summary>
public static class XLog
{
static string rootFolderPattern = null;
static string? rootFolderPattern = null;
#if WINDOWS_UWP
static LoggingChannel loggingChannel;
#endif
Expand All @@ -22,7 +22,7 @@ public static class XLog
/// Call this before logging starts.
/// </summary>
/// <param name="rootFolderPattern">Should match the top folder name(s) within the source control repository, e.g. @"\MobileRealtimePush\MobileRealtimePush\". Any folders before the first match of this pattern are omitted from the logged source file paths</param>
public static void Init(string rootFolderPattern = null)
public static void Init(string? rootFolderPattern = null)
{
XLog.rootFolderPattern = rootFolderPattern;
#if WINDOWS_UWP
Expand All @@ -46,10 +46,10 @@ public static void Init(string rootFolderPattern = null)
/// <param name="sourceLineNumber">supplied by compiler, no need to specify in code unless you want to pass a deeper call context</param>
[Conditional("DEBUG")]
public static void Debug(
object data = null,
string tag = null,
[CallerMemberName] string memberName = null,
[CallerFilePath] string sourceFilePath = null,
object? data = null,
string? tag = null,
[CallerMemberName] string? memberName = null,
[CallerFilePath] string? sourceFilePath = null,
[CallerLineNumber] int sourceLineNumber = -1)
{
var message = FormatLogString(data, tag, memberName, sourceFilePath, sourceLineNumber);
Expand All @@ -75,10 +75,10 @@ public static void Debug(
/// <param name="sourceLineNumber">supplied by compiler, no need to specify in code unless you want to pass a deeper call context</param>
[Conditional("TRACE")]
public static void Trace(
object data = null,
string tag = null,
[CallerMemberName] string memberName = null,
[CallerFilePath] string sourceFilePath = null,
object? data = null,
string? tag = null,
[CallerMemberName] string? memberName = null,
[CallerFilePath] string? sourceFilePath = null,
[CallerLineNumber] int sourceLineNumber = -1)
{
var message = FormatLogString(data, tag, memberName, sourceFilePath, sourceLineNumber);
Expand All @@ -90,9 +90,9 @@ public static void Trace(
#endif
}

public static string TruncateAt(this string s, int maxLength, string truncatedSuffix = "...") => s?.Length <= maxLength ? s : s.Substring(0, maxLength) + truncatedSuffix;
public static string TruncateAt(this string? s, int maxLength, string truncatedSuffix = "...") => s?.Length <= maxLength ? s : s?.Substring(0, maxLength) + truncatedSuffix;

static string FormatLogString(object data = null, string tag = null, string memberName = null, string sourceFilePath = null, int sourceLineNumber = -1)
static string FormatLogString(object? data, string? tag, string? memberName, string? sourceFilePath, int sourceLineNumber)
{
var line = new StringBuilder();

Expand Down Expand Up @@ -121,7 +121,7 @@ static string FormatLogString(object data = null, string tag = null, string memb
line.Append(dataString);
}

if (!string.IsNullOrEmpty(sourceFilePath))
if (sourceFilePath != null && !string.IsNullOrEmpty(sourceFilePath))
{
if (!string.IsNullOrEmpty(rootFolderPattern))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
{
public partial class BoolToObjectConverterPage
public partial class BoolToObjectConverterPage
{
public BoolToObjectConverterPage()
{
InitializeComponent();
}
public BoolToObjectConverterPage() => InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
{
public partial class DoubleToIntConverterPage
public partial class DoubleToIntConverterPage
{
public DoubleToIntConverterPage()
{
InitializeComponent();
}
public DoubleToIntConverterPage() => InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
{
public partial class EnumToBoolConverterPage
{
public EnumToBoolConverterPage()
{
InitializeComponent();
}
}
public partial class EnumToBoolConverterPage
{
public EnumToBoolConverterPage() => InitializeComponent();
}
}
Loading