fix: improve nullable annotation.

This commit is contained in:
rabbitism 2023-03-26 19:38:12 +08:00
parent e193bfc697
commit 9f43baa039
2 changed files with 30 additions and 25 deletions

View File

@ -14,19 +14,19 @@ public class FunctionalColorGroupControl: TemplatedControl
set => SetValue(TitleProperty, value); set => SetValue(TitleProperty, value);
} }
public static readonly DirectProperty<FunctionalColorGroupControl, IEnumerable> LightColorsProperty = AvaloniaProperty.RegisterDirect<FunctionalColorGroupControl, IEnumerable>( public static readonly DirectProperty<FunctionalColorGroupControl, IEnumerable?> LightColorsProperty = AvaloniaProperty.RegisterDirect<FunctionalColorGroupControl, IEnumerable?>(
nameof(LightColors), o => o.LightColors, (o, v) => o.LightColors = v); nameof(LightColors), o => o.LightColors, (o, v) => o.LightColors = v);
private IEnumerable _lightColors; private IEnumerable? _lightColors;
public IEnumerable LightColors public IEnumerable? LightColors
{ {
get => _lightColors; get => _lightColors;
set => SetAndRaise(LightColorsProperty, ref _lightColors, value); set => SetAndRaise(LightColorsProperty, ref _lightColors, value);
} }
public static readonly DirectProperty<FunctionalColorGroupControl, IEnumerable> DarkColorsProperty = AvaloniaProperty.RegisterDirect<FunctionalColorGroupControl, IEnumerable>( public static readonly DirectProperty<FunctionalColorGroupControl, IEnumerable?> DarkColorsProperty = AvaloniaProperty.RegisterDirect<FunctionalColorGroupControl, IEnumerable?>(
nameof(DarkColors), o => o.DarkColors, (o, v) => o.DarkColors = v); nameof(DarkColors), o => o.DarkColors, (o, v) => o.DarkColors = v);
private IEnumerable _darkColors; private IEnumerable? _darkColors;
public IEnumerable DarkColors public IEnumerable? DarkColors
{ {
get => _darkColors; get => _darkColors;
set => SetAndRaise(DarkColorsProperty, ref _darkColors, value); set => SetAndRaise(DarkColorsProperty, ref _darkColors, value);

View File

@ -12,10 +12,10 @@ namespace Semi.Avalonia.Demo.ViewModels;
public class PaletteDemoViewModel: ObservableObject public class PaletteDemoViewModel: ObservableObject
{ {
private readonly string[] _predefinedColorNames = { "Amber","Blue","Cyan","Green","Grey","Indigo","LightBlue","LightGreen","Lime","Orange","Pink","Purple","Red","Teal","Violet","Yellow" }; private readonly string[] _predefinedColorNames = { "Amber","Blue","Cyan","Green","Grey","Indigo","LightBlue","LightGreen","Lime","Orange","Pink","Purple","Red","Teal","Violet","Yellow" };
private IResourceDictionary _lightResourceDictionary; private readonly IResourceDictionary? _lightResourceDictionary;
private IResourceDictionary _darkResourceDictionary; private readonly IResourceDictionary? _darkResourceDictionary;
private ColorItemViewModel _selectedColor; private ColorItemViewModel _selectedColor = null!;
public ColorItemViewModel SelectedColor public ColorItemViewModel SelectedColor
{ {
@ -24,14 +24,14 @@ public class PaletteDemoViewModel: ObservableObject
} }
private ObservableCollection<ColorListViewModel> _lightLists; private ObservableCollection<ColorListViewModel>? _lightLists;
public ObservableCollection<ColorListViewModel> LightLists public ObservableCollection<ColorListViewModel>? LightLists
{ {
get => _lightLists; get => _lightLists;
set => SetProperty(ref _lightLists, value); set => SetProperty(ref _lightLists, value);
} }
private ObservableCollection<ColorListViewModel> _darkLists; private ObservableCollection<ColorListViewModel>? _darkLists;
public ObservableCollection<ColorListViewModel> DarkLists public ObservableCollection<ColorListViewModel>? DarkLists
{ {
get => _darkLists; get => _darkLists;
set => SetProperty(ref _darkLists, value); set => SetProperty(ref _darkLists, value);
@ -41,8 +41,8 @@ public class PaletteDemoViewModel: ObservableObject
public PaletteDemoViewModel() public PaletteDemoViewModel()
{ {
_lightResourceDictionary = (ResourceDictionary)AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Light/Palette.axaml")); _lightResourceDictionary = AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Light/Palette.axaml")) as ResourceDictionary;
_darkResourceDictionary = (ResourceDictionary)AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Dark/Palette.axaml")); _darkResourceDictionary = AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Dark/Palette.axaml")) as ResourceDictionary;
WeakReferenceMessenger.Default.Register<PaletteDemoViewModel, ColorItemViewModel>(this, OnClickColorItem); WeakReferenceMessenger.Default.Register<PaletteDemoViewModel, ColorItemViewModel>(this, OnClickColorItem);
} }
@ -110,8 +110,12 @@ public class ColorListViewModel: ObservableObject
set => SetProperty(ref _seriesName, value); set => SetProperty(ref _seriesName, value);
} }
internal void Initialize(IResourceDictionary resourceDictionary, string color, bool light) internal void Initialize(IResourceDictionary? resourceDictionary, string color, bool light)
{ {
if (resourceDictionary is null)
{
return;
}
SeriesName = color; SeriesName = color;
Color = new ObservableCollection<ColorItemViewModel>(); Color = new ObservableCollection<ColorItemViewModel>();
@ -134,28 +138,28 @@ public class ColorListViewModel: ObservableObject
public class ColorItemViewModel : ObservableObject public class ColorItemViewModel : ObservableObject
{ {
private IBrush _brush; private IBrush _brush = null!;
public IBrush Brush public IBrush Brush
{ {
get => _brush; get => _brush;
set => SetProperty(ref _brush, value); set => SetProperty(ref _brush, value);
} }
private IBrush _textBrush; private IBrush _textBrush = null!;
public IBrush TextBrush public IBrush TextBrush
{ {
get => _textBrush; get => _textBrush;
set => SetProperty(ref _textBrush, value); set => SetProperty(ref _textBrush, value);
} }
private string _colorDisplayName; private string _colorDisplayName = null!;
public string ColorDisplayName public string ColorDisplayName
{ {
get => _colorDisplayName; get => _colorDisplayName;
set => SetProperty(ref _colorDisplayName, value); set => SetProperty(ref _colorDisplayName, value);
} }
private string _resourceKey; private string _resourceKey = null!;
public string ResourceKey public string ResourceKey
{ {
@ -163,7 +167,7 @@ public class ColorItemViewModel : ObservableObject
set => SetProperty(ref _resourceKey, value); set => SetProperty(ref _resourceKey, value);
} }
private string _hex; private string _hex = null!;
public string Hex public string Hex
{ {
@ -190,7 +194,7 @@ public class ColorItemViewModel : ObservableObject
public class FunctionalColorGroupViewModel : ObservableObject public class FunctionalColorGroupViewModel : ObservableObject
{ {
private string _title; private string _title = null!;
public string Title public string Title
{ {
get => _title; get => _title;
@ -200,21 +204,22 @@ public class FunctionalColorGroupViewModel : ObservableObject
public ObservableCollection<ColorItemViewModel> LightColors { get; set; } = new(); public ObservableCollection<ColorItemViewModel> LightColors { get; set; } = new();
public ObservableCollection<ColorItemViewModel> DarkColors { get; set; } = new(); public ObservableCollection<ColorItemViewModel> DarkColors { get; set; } = new();
public FunctionalColorGroupViewModel(string title, IResourceDictionary lightDictionary, IResourceDictionary darkDictionary, IReadOnlyList<Tuple<string, string>> tokens) public FunctionalColorGroupViewModel(string title, IResourceDictionary? lightDictionary, IResourceDictionary? darkDictionary, IReadOnlyList<Tuple<string, string>> tokens)
{ {
Title = title; Title = title;
foreach (var token in tokens) foreach (var token in tokens)
{ {
string key = token.Item1; string key = token.Item1;
string name = token.Item2; string name = token.Item2;
if (lightDictionary.TryGetValue(key, out var lightValue)) if (lightDictionary?.TryGetValue(key, out var lightValue) ?? false)
{ {
if (lightValue is ISolidColorBrush lightBrush) if (lightValue is ISolidColorBrush lightBrush)
{ {
LightColors.Add(new ColorItemViewModel(name, lightBrush, key, true, 0)); LightColors.Add(new ColorItemViewModel(name, lightBrush, key, true, 0));
} }
} }
if (darkDictionary.TryGetValue(key, out var darkValue))
if (darkDictionary?.TryGetValue(key, out var darkValue) ?? false)
{ {
if (darkValue is ISolidColorBrush darkBrush) if (darkValue is ISolidColorBrush darkBrush)
{ {