mirror of
https://gitcode.com/gh_mirrors/se/Semi.Avalonia
synced 2026-04-26 03:07:54 +08:00
feat: implement singleton pattern for PaletteDemoViewModel and add initialization check
This commit is contained in:
@@ -12,14 +12,16 @@ public partial class PaletteDemo : UserControl
|
|||||||
public PaletteDemo()
|
public PaletteDemo()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
this.DataContext = new PaletteDemoViewModel();
|
this.DataContext = PaletteDemoViewModel.Instance.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async void OnApplyTemplate(TemplateAppliedEventArgs e)
|
protected override async void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnApplyTemplate(e);
|
base.OnApplyTemplate(e);
|
||||||
PaletteDemoViewModel? vm = this.DataContext as PaletteDemoViewModel;
|
if (this.DataContext is PaletteDemoViewModel vm && !vm.IsInitialized)
|
||||||
await Dispatcher.UIThread.InvokeAsync(() => { vm?.InitializeResources(); });
|
{
|
||||||
|
await Dispatcher.UIThread.InvokeAsync(() => { vm?.InitializeResources(); });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Copy(object? o)
|
public async Task Copy(object? o)
|
||||||
@@ -31,4 +33,4 @@ public partial class PaletteDemo : UserControl
|
|||||||
await c.SetTextAsync(o.ToString());
|
await c.SetTextAsync(o.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ namespace Semi.Avalonia.Demo.ViewModels;
|
|||||||
|
|
||||||
public partial class PaletteDemoViewModel : ObservableObject
|
public partial class PaletteDemoViewModel : ObservableObject
|
||||||
{
|
{
|
||||||
|
public static Lazy<PaletteDemoViewModel> Instance { get; } = new(() => new PaletteDemoViewModel());
|
||||||
|
public bool IsInitialized { get; private set; }
|
||||||
private readonly string[] _predefinedColorNames =
|
private readonly string[] _predefinedColorNames =
|
||||||
[
|
[
|
||||||
"Red", "Pink", "Purple", "Violet", "Indigo",
|
"Red", "Pink", "Purple", "Violet", "Indigo",
|
||||||
@@ -44,6 +46,7 @@ public partial class PaletteDemoViewModel : ObservableObject
|
|||||||
InitializePalette();
|
InitializePalette();
|
||||||
InitializeFunctionalColors();
|
InitializeFunctionalColors();
|
||||||
InitializeShadows();
|
InitializeShadows();
|
||||||
|
IsInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitializePalette()
|
private void InitializePalette()
|
||||||
@@ -289,4 +292,4 @@ public partial class ShadowGroupViewModel : ObservableObject
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user