diff --git a/demo/Semi.Avalonia.Demo/Controls/FunctionalColorGroupControl.cs b/demo/Semi.Avalonia.Demo/Controls/FunctionalColorGroupControl.cs index b7bbb44..7a17dee 100644 --- a/demo/Semi.Avalonia.Demo/Controls/FunctionalColorGroupControl.cs +++ b/demo/Semi.Avalonia.Demo/Controls/FunctionalColorGroupControl.cs @@ -14,19 +14,19 @@ public class FunctionalColorGroupControl: TemplatedControl set => SetValue(TitleProperty, value); } - public static readonly DirectProperty LightColorsProperty = AvaloniaProperty.RegisterDirect( + public static readonly DirectProperty LightColorsProperty = AvaloniaProperty.RegisterDirect( nameof(LightColors), o => o.LightColors, (o, v) => o.LightColors = v); - private IEnumerable _lightColors; - public IEnumerable LightColors + private IEnumerable? _lightColors; + public IEnumerable? LightColors { get => _lightColors; set => SetAndRaise(LightColorsProperty, ref _lightColors, value); } - public static readonly DirectProperty DarkColorsProperty = AvaloniaProperty.RegisterDirect( + public static readonly DirectProperty DarkColorsProperty = AvaloniaProperty.RegisterDirect( nameof(DarkColors), o => o.DarkColors, (o, v) => o.DarkColors = v); - private IEnumerable _darkColors; - public IEnumerable DarkColors + private IEnumerable? _darkColors; + public IEnumerable? DarkColors { get => _darkColors; set => SetAndRaise(DarkColorsProperty, ref _darkColors, value); diff --git a/demo/Semi.Avalonia.Demo/ViewModels/PaletteDemoViewModel.cs b/demo/Semi.Avalonia.Demo/ViewModels/PaletteDemoViewModel.cs index 33f98f1..75fe787 100644 --- a/demo/Semi.Avalonia.Demo/ViewModels/PaletteDemoViewModel.cs +++ b/demo/Semi.Avalonia.Demo/ViewModels/PaletteDemoViewModel.cs @@ -12,10 +12,10 @@ namespace Semi.Avalonia.Demo.ViewModels; 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 IResourceDictionary _lightResourceDictionary; - private IResourceDictionary _darkResourceDictionary; + private readonly IResourceDictionary? _lightResourceDictionary; + private readonly IResourceDictionary? _darkResourceDictionary; - private ColorItemViewModel _selectedColor; + private ColorItemViewModel _selectedColor = null!; public ColorItemViewModel SelectedColor { @@ -24,14 +24,14 @@ public class PaletteDemoViewModel: ObservableObject } - private ObservableCollection _lightLists; - public ObservableCollection LightLists + private ObservableCollection? _lightLists; + public ObservableCollection? LightLists { get => _lightLists; set => SetProperty(ref _lightLists, value); } - private ObservableCollection _darkLists; - public ObservableCollection DarkLists + private ObservableCollection? _darkLists; + public ObservableCollection? DarkLists { get => _darkLists; set => SetProperty(ref _darkLists, value); @@ -41,8 +41,8 @@ public class PaletteDemoViewModel: ObservableObject public PaletteDemoViewModel() { - _lightResourceDictionary = (ResourceDictionary)AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Light/Palette.axaml")); - _darkResourceDictionary = (ResourceDictionary)AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Dark/Palette.axaml")); + _lightResourceDictionary = AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Light/Palette.axaml")) as ResourceDictionary; + _darkResourceDictionary = AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Dark/Palette.axaml")) as ResourceDictionary; WeakReferenceMessenger.Default.Register(this, OnClickColorItem); } @@ -110,8 +110,12 @@ public class ColorListViewModel: ObservableObject 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; Color = new ObservableCollection(); @@ -134,28 +138,28 @@ public class ColorListViewModel: ObservableObject public class ColorItemViewModel : ObservableObject { - private IBrush _brush; + private IBrush _brush = null!; public IBrush Brush { get => _brush; set => SetProperty(ref _brush, value); } - private IBrush _textBrush; + private IBrush _textBrush = null!; public IBrush TextBrush { get => _textBrush; set => SetProperty(ref _textBrush, value); } - private string _colorDisplayName; + private string _colorDisplayName = null!; public string ColorDisplayName { get => _colorDisplayName; set => SetProperty(ref _colorDisplayName, value); } - private string _resourceKey; + private string _resourceKey = null!; public string ResourceKey { @@ -163,7 +167,7 @@ public class ColorItemViewModel : ObservableObject set => SetProperty(ref _resourceKey, value); } - private string _hex; + private string _hex = null!; public string Hex { @@ -190,7 +194,7 @@ public class ColorItemViewModel : ObservableObject public class FunctionalColorGroupViewModel : ObservableObject { - private string _title; + private string _title = null!; public string Title { get => _title; @@ -200,21 +204,22 @@ public class FunctionalColorGroupViewModel : ObservableObject public ObservableCollection LightColors { get; set; } = new(); public ObservableCollection DarkColors { get; set; } = new(); - public FunctionalColorGroupViewModel(string title, IResourceDictionary lightDictionary, IResourceDictionary darkDictionary, IReadOnlyList> tokens) + public FunctionalColorGroupViewModel(string title, IResourceDictionary? lightDictionary, IResourceDictionary? darkDictionary, IReadOnlyList> tokens) { Title = title; foreach (var token in tokens) { string key = token.Item1; string name = token.Item2; - if (lightDictionary.TryGetValue(key, out var lightValue)) + if (lightDictionary?.TryGetValue(key, out var lightValue) ?? false) { if (lightValue is ISolidColorBrush lightBrush) { 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) {