Browse Source
Merge pull request #11843 from AvaloniaUI/fix-palette-getresourcec-code
Fix palette TryGetResource code
pull/11971/head
Max Katz
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with
20 additions and
7 deletions
-
samples/ControlCatalog/App.xaml
-
samples/ControlCatalog/App.xaml.cs
-
src/Avalonia.Controls/AppBuilder.cs
-
src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs
|
|
|
@ -28,6 +28,9 @@ |
|
|
|
</ResourceDictionary.ThemeDictionaries> |
|
|
|
|
|
|
|
<!-- Styles attached dynamically depending on current theme (simple or fluent) --> |
|
|
|
<FluentTheme x:Key="FluentTheme"> |
|
|
|
</FluentTheme> |
|
|
|
<SimpleTheme x:Key="SimpleTheme" /> |
|
|
|
<StyleInclude x:Key="DataGridFluent" Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml" /> |
|
|
|
<StyleInclude x:Key="DataGridSimple" Source="avares://Avalonia.Controls.DataGrid/Themes/Simple.xaml" /> |
|
|
|
<StyleInclude x:Key="ColorPickerFluent" Source="avares://Avalonia.Controls.ColorPicker/Themes/Fluent/Fluent.xaml" /> |
|
|
|
|
|
|
|
@ -30,8 +30,8 @@ namespace ControlCatalog |
|
|
|
|
|
|
|
AvaloniaXamlLoader.Load(this); |
|
|
|
|
|
|
|
_fluentTheme = new FluentTheme(); |
|
|
|
_simpleTheme = new SimpleTheme(); |
|
|
|
_fluentTheme = (FluentTheme)Resources["FluentTheme"]!; |
|
|
|
_simpleTheme = (SimpleTheme)Resources["SimpleTheme"]!; |
|
|
|
_colorPickerFluent = (IStyle)Resources["ColorPickerFluent"]!; |
|
|
|
_colorPickerSimple = (IStyle)Resources["ColorPickerSimple"]!; |
|
|
|
_dataGridFluent = (IStyle)Resources["DataGridFluent"]!; |
|
|
|
|
|
|
|
@ -151,7 +151,7 @@ namespace Avalonia |
|
|
|
} |
|
|
|
|
|
|
|
throw new InvalidOperationException( |
|
|
|
$"Unable to create AppBuilder from type {entryPointType.Name}." + |
|
|
|
$"Unable to create AppBuilder from type \"{entryPointType.FullName}\". " + |
|
|
|
$"Input type either needs to have BuildAvaloniaApp -> AppBuilder method or inherit Application type."); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -10,12 +10,18 @@ internal class ColorPaletteResourcesCollection : AvaloniaDictionary<ThemeVariant |
|
|
|
public ColorPaletteResourcesCollection() : base(2) |
|
|
|
{ |
|
|
|
this.ForEachItem( |
|
|
|
(_, x) => |
|
|
|
(key, x) => |
|
|
|
{ |
|
|
|
if (Owner is not null) |
|
|
|
{ |
|
|
|
x.PropertyChanged += Palette_PropertyChanged; |
|
|
|
} |
|
|
|
|
|
|
|
if (key != ThemeVariant.Dark && key != ThemeVariant.Light) |
|
|
|
{ |
|
|
|
throw new InvalidOperationException( |
|
|
|
$"{nameof(FluentTheme)}.{nameof(FluentTheme.Palettes)} only supports Light and Dark variants."); |
|
|
|
} |
|
|
|
}, |
|
|
|
(_, x) => |
|
|
|
{ |
|
|
|
@ -30,9 +36,13 @@ internal class ColorPaletteResourcesCollection : AvaloniaDictionary<ThemeVariant |
|
|
|
public bool HasResources => Count > 0; |
|
|
|
public bool TryGetResource(object key, ThemeVariant? theme, out object? value) |
|
|
|
{ |
|
|
|
theme ??= ThemeVariant.Default; |
|
|
|
if (base.TryGetValue(theme, out var paletteResources) |
|
|
|
&& paletteResources.TryGetResource(key, theme, out value)) |
|
|
|
if (theme == null || theme == ThemeVariant.Default) |
|
|
|
{ |
|
|
|
theme = ThemeVariant.Light; |
|
|
|
} |
|
|
|
|
|
|
|
if (base.TryGetValue(theme, out var themePaletteResources) |
|
|
|
&& themePaletteResources.TryGetResource(key, theme, out value)) |
|
|
|
{ |
|
|
|
return true; |
|
|
|
} |
|
|
|
|