Skip to content

Commit

Permalink
Nullable review
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 committed Jul 7, 2021
1 parent 0f9ad64 commit ddf9ea9
Show file tree
Hide file tree
Showing 14 changed files with 723 additions and 617 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@ public partial class MultiSelectionComboBoxExample : UserControl
{
public MultiSelectionComboBoxExample()
{
InitializeComponent();
this.InitializeComponent();
}

private void Mscb_Example_AddingItem(object sender, AddingItemEventArgs args)
private void Mscb_Example_AddingItem(object? sender, AddingItemEventArgs args)
{
// We don't want to get double entries so let`s check if we already have one.
args.Accepted = !args.TargetList.Contains(args.ParsedObject);
args.Accepted = args.TargetList is not null && !args.TargetList.Contains(args.ParsedObject);
}

private async void Mscb_Example_AddedItem(object sender, AddedItemEventArgs args)
private async void Mscb_Example_AddedItem(object? sender, AddedItemEventArgs args)
{
await this.TryFindParent<MetroWindow>()?.ShowMessageAsync("Added Item", $"Successfully added \"{args.AddedItem}\" to your Items-Collection");
var window = this.TryFindParent<MetroWindow>();
if (window is null)
{
return;
}

await window.ShowMessageAsync("Added Item", $"Successfully added \"{args.AddedItem}\" to your Items-Collection");
}

private void mscb_Example_SelectionChanged(object sender, SelectionChangedEventArgs e)
Expand All @@ -43,4 +49,4 @@ private void mscb_Example_SelectionChanged(object sender, SelectionChangedEventA
}
}
}
}
}
312 changes: 156 additions & 156 deletions src/MahApps.Metro.Samples/MahApps.Metro.Demo/MainWindowViewModel.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public virtual Artist? Artist

public override string ToString()
{
return $"{Artist}: {Title} ({Price.ToString("C")})";
return $"{this.Artist}: {this.Title} ({this.Price.ToString("C")})";
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ public List<Album>? Albums
set => this.Set(ref this._albums, value);
}

#if NET5_0_OR_GREATER || NETCOREAPP
public override string? ToString()
#else
public override string ToString()
#endif
{
return Name;
return this.Name ?? base.ToString();
}
}
}
6 changes: 5 additions & 1 deletion src/MahApps.Metro.Samples/MahApps.Metro.Demo/Models/Genre.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ public List<Album>? Albums
set => this.Set(ref this._albums, value);
}

#if NET5_0_OR_GREATER || NETCOREAPP
public override string? ToString()
#else
public override string ToString()
#endif
{
return Name;
return this.Name ?? base.ToString();
}
}
}
27 changes: 16 additions & 11 deletions src/MahApps.Metro.Samples/MahApps.Metro.Demo/Models/ObjectParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ public class ObjectParser : IParseStringToObject
private readonly MainWindowViewModel _mainWindowViewModel;
private readonly IDialogCoordinator _dialogCoordinator;

public ObjectParser (MainWindowViewModel mainWindowViewModel, IDialogCoordinator dialogCoordinator)
public ObjectParser(MainWindowViewModel mainWindowViewModel, IDialogCoordinator dialogCoordinator)
{
this._mainWindowViewModel = mainWindowViewModel;
this._dialogCoordinator = dialogCoordinator;
}

public bool TryCreateObjectFromString(string input, out object result, CultureInfo culture = null, string stringFormat = null, Type elementType = null)

/// <inheritdoc />
public bool TryCreateObjectFromString(string? input,
out object? result,
CultureInfo? culture = null,
string? stringFormat = null,
Type? elementType = null)
{
if (string.IsNullOrWhiteSpace(input))
{
Expand All @@ -29,15 +34,15 @@ public bool TryCreateObjectFromString(string input, out object result, CultureIn
}

MetroDialogSettings dialogSettings = new MetroDialogSettings
{
AffirmativeButtonText = "Yes",
NegativeButtonText = "No",
DefaultButtonFocus = MessageDialogResult.Affirmative
};
{
AffirmativeButtonText = "Yes",
NegativeButtonText = "No",
DefaultButtonFocus = MessageDialogResult.Affirmative
};

if (this._dialogCoordinator.ShowModalMessageExternal(this._mainWindowViewModel, "Add Animal", $"Do you want to add \"{input}\" to the animals list?", MessageDialogStyle.AffirmativeAndNegative, dialogSettings) == MessageDialogResult.Affirmative)
if (this._dialogCoordinator.ShowModalMessageExternal(this._mainWindowViewModel, "Add Animal", $"Do you want to add \"{input}\" to the animals list?", MessageDialogStyle.AffirmativeAndNegative, dialogSettings) == MessageDialogResult.Affirmative)
{
result = input.Trim();
result = input!.Trim();
return true;
}
else
Expand All @@ -47,4 +52,4 @@ public bool TryCreateObjectFromString(string input, out object result, CultureIn
}
}
}
}
}
16 changes: 11 additions & 5 deletions src/MahApps.Metro/Controls/Helper/BindingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private static readonly DependencyProperty DummyTextProperty
/// <param name="source">the object to evaluate</param>
/// <param name="expression">the binding expression to evaluate</param>
/// <returns>the result of the <see cref="Binding"/></returns>
public static object Eval(object source, string expression)
public static object? Eval(object? source, string? expression)
{
Binding binding = new Binding(expression) { Source = source };
return Eval(binding);
Expand All @@ -50,7 +50,7 @@ public static object Eval(object source, string expression)
/// <param name="expression">the binding expression to evaluate</param>
/// <param name="format">the string format to use</param>
/// <returns>the result of the <see cref="Binding"/></returns>
public static object Eval(object source, string expression, string format)
public static object? Eval(object? source, string? expression, string? format)
{
Binding binding = new Binding(expression) { Source = source, StringFormat = format };
return Eval(binding);
Expand All @@ -62,7 +62,7 @@ public static object Eval(object source, string expression, string format)
/// <param name="binding">The <see cref="Binding"/> to evaluate</param>
/// <param name="source">the object to evaluate</param>
/// <returns></returns>
public static object Eval(Binding binding, object source)
public static object? Eval(Binding? binding, object? source)
{
if (binding is null)
{
Expand All @@ -85,6 +85,7 @@ public static object Eval(Binding binding, object source)
StringFormat = binding.StringFormat,
TargetNullValue = binding.TargetNullValue
};

return Eval(newBinding);
}

Expand All @@ -94,8 +95,13 @@ public static object Eval(Binding binding, object source)
/// <param name="binding">The <see cref="Binding"/> to evaluate</param>
/// <param name="dependencyObject">optional: The <see cref="DependencyObject"/> to evaluate</param>
/// <returns>The resulting object</returns>
public static object Eval(Binding binding, DependencyObject dependencyObject)
public static object? Eval(Binding? binding, DependencyObject? dependencyObject)
{
if (binding is null)
{
throw new ArgumentNullException(nameof(binding));
}

dependencyObject ??= new DependencyObject();

if (string.IsNullOrEmpty(binding.StringFormat))
Expand All @@ -115,7 +121,7 @@ public static object Eval(Binding binding, DependencyObject dependencyObject)
/// </summary>
/// <param name="binding">The <see cref="Binding"/> to evaluate</param>
/// <returns>The resulting object</returns>
public static object Eval(Binding binding)
public static object? Eval(Binding? binding)
{
return Eval(binding, null);
}
Expand Down
5 changes: 2 additions & 3 deletions src/MahApps.Metro/Controls/Helper/TextBoxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,11 +1109,11 @@ public static void ButtonClicked(object sender, RoutedEventArgs e)
switch (multiSelectionComboBox.SelectionMode)
{
case SelectionMode.Single:
multiSelectionComboBox.SelectedItem = null;
multiSelectionComboBox.SetCurrentValue(MultiSelectionComboBox.SelectedItemProperty, null);
break;
case SelectionMode.Multiple:
case SelectionMode.Extended:
multiSelectionComboBox.SelectedItems.Clear();
multiSelectionComboBox.SelectedItems?.Clear();
break;
default:
throw new NotSupportedException("Unknown SelectionMode");
Expand All @@ -1135,7 +1135,6 @@ public static void ButtonClicked(object sender, RoutedEventArgs e)
{
colorPicker.SetCurrentValue(ColorPickerBase.SelectedColorProperty, null);
}

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MahApps.Metro.Controls
Expand All @@ -16,32 +15,33 @@ public class AddedItemEventArgs : RoutedEventArgs
/// <summary>
/// Initializes a new instance of the <see cref="AddedItemEventArgs"/> class.
/// </summary>
/// <param name="routedEvent">The AddedItemEvent/param>
/// <param name="routedEvent">The AddedItemEvent</param>
/// <param name="source">The source object</param>
/// <param name="addedItem">The added object</param>
/// <param name="targetList">The target <see cref="IList"/> where the <see cref="AddedItem"/> should be added</param>
public AddedItemEventArgs(RoutedEvent routedEvent,
object source,
object addedItem,
IList targetList) : base(routedEvent, source)
object source,
object? addedItem,
IList? targetList)
: base(routedEvent, source)
{
AddedItem = addedItem;
TargetList = targetList;
this.AddedItem = addedItem;
this.TargetList = targetList;
}

/// <summary>
/// Gets the added item
/// </summary>
public object AddedItem { get; }
public object? AddedItem { get; }

/// <summary>
/// Gets the <see cref="IList"/> where the <see cref="AddedItem"/> was added to
/// </summary>
public IList TargetList { get; }
public IList? TargetList { get; }
}

/// <summary>
/// RoutedEventHandler used for the <see cref="MultiSelectionComboBox.AddedItemEvent"/>.
/// </summary>
public delegate void AddedItemEventArgsHandler(object sender, AddedItemEventArgs args);
}
public delegate void AddedItemEventArgsHandler(object? sender, AddedItemEventArgs args);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections;
using System.Globalization;
using System.Windows;
Expand All @@ -13,7 +17,7 @@ public class AddingItemEventArgs : RoutedEventArgs
/// <summary>
/// Initializes a new instance of the <see cref="AddingItemEventArgs"/> class.
/// </summary>
/// <param name="routedEvent">The AddingItemEvent/param>
/// <param name="routedEvent">The AddingItemEvent</param>
/// <param name="source">The source object</param>
/// <param name="input">The input string to parse</param>
/// <param name="parsedObject">The parsed object</param>
Expand All @@ -25,39 +29,40 @@ public class AddingItemEventArgs : RoutedEventArgs
/// <param name="parser">The used parser</param>
public AddingItemEventArgs(RoutedEvent routedEvent,
object source,
string input,
object parsedObject,
string? input,
object? parsedObject,
bool accepted,
IList targetList,
Type targetType,
string stringFormat,
IList? targetList,
Type? targetType,
string? stringFormat,
CultureInfo culture,
IParseStringToObject parser): base(routedEvent, source)
IParseStringToObject? parser)
: base(routedEvent, source)
{
Input = input;
ParsedObject = parsedObject;
Accepted = accepted;
TargetList = targetList;
TargetType = targetType;
StringFormat = stringFormat;
Culture = culture;
Parser = parser;
this.Input = input;
this.ParsedObject = parsedObject;
this.Accepted = accepted;
this.TargetList = targetList;
this.TargetType = targetType;
this.StringFormat = stringFormat;
this.Culture = culture;
this.Parser = parser;
}

/// <summary>
/// The Textinput to parse
/// The text input to parse
/// </summary>
public string Input { get; }
public string? Input { get; }

/// <summary>
/// Gets or sets the parsed object to add. You can override it
/// </summary>
public object ParsedObject { get; set; }
public object? ParsedObject { get; set; }

/// <summary>
/// Gets the string format which can be used to control the <see cref="Parser"/>
/// </summary>
public string StringFormat { get; }
public string? StringFormat { get; }

/// <summary>
/// Gets the culture which can be used to control the <see cref="Parser"/>
Expand All @@ -67,26 +72,26 @@ public AddingItemEventArgs(RoutedEvent routedEvent,
/// <summary>
/// Gets the <see cref="IParseStringToObject"/>-Instance which was used to parse the <see cref="Input"/> to the <see cref="ParsedObject"/>
/// </summary>
public IParseStringToObject Parser { get; }
public IParseStringToObject? Parser { get; }

/// <summary>
/// Gets the target <see cref="Type"/> to which the <see cref="Input"/> should be converted to
/// </summary>
public Type TargetType { get; }
public Type? TargetType { get; }

/// <summary>
/// Gets the <see cref="IList"/> where the <see cref="ParsedObject"/> should be added
/// </summary>
public IList TargetList { get; }
public IList? TargetList { get; }

/// <summary>
/// Gets or sets wether the <see cref="ParsedObject"/> is accepted and can be added
/// Gets or sets whether the <see cref="ParsedObject"/> is accepted and can be added
/// </summary>
public bool Accepted { get; set; }
}

/// <summary>
/// RoutedEventHandler used for the <see cref="MultiSelectionComboBox.AddingItemEvent"/>.
/// </summary>
public delegate void AddingItemEventArgsHandler(object sender, AddingItemEventArgs args);
}
public delegate void AddingItemEventArgsHandler(object? sender, AddingItemEventArgs args);
}
Loading

0 comments on commit ddf9ea9

Please sign in to comment.