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()
|
||||
{
|
||||
InitializeComponent();
|
||||
this.DataContext = new PaletteDemoViewModel();
|
||||
this.DataContext = PaletteDemoViewModel.Instance.Value;
|
||||
}
|
||||
|
||||
protected override async void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
PaletteDemoViewModel? vm = this.DataContext as PaletteDemoViewModel;
|
||||
await Dispatcher.UIThread.InvokeAsync(() => { vm?.InitializeResources(); });
|
||||
if (this.DataContext is PaletteDemoViewModel vm && !vm.IsInitialized)
|
||||
{
|
||||
await Dispatcher.UIThread.InvokeAsync(() => { vm?.InitializeResources(); });
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Copy(object? o)
|
||||
@@ -31,4 +33,4 @@ public partial class PaletteDemo : UserControl
|
||||
await c.SetTextAsync(o.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ namespace Semi.Avalonia.Demo.ViewModels;
|
||||
|
||||
public partial class PaletteDemoViewModel : ObservableObject
|
||||
{
|
||||
public static Lazy<PaletteDemoViewModel> Instance { get; } = new(() => new PaletteDemoViewModel());
|
||||
public bool IsInitialized { get; private set; }
|
||||
private readonly string[] _predefinedColorNames =
|
||||
[
|
||||
"Red", "Pink", "Purple", "Violet", "Indigo",
|
||||
@@ -44,6 +46,7 @@ public partial class PaletteDemoViewModel : ObservableObject
|
||||
InitializePalette();
|
||||
InitializeFunctionalColors();
|
||||
InitializeShadows();
|
||||
IsInitialized = true;
|
||||
}
|
||||
|
||||
private void InitializePalette()
|
||||
@@ -289,4 +292,4 @@ public partial class ShadowGroupViewModel : ObservableObject
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user