-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathControlPageCard.cs
More file actions
83 lines (69 loc) · 2.5 KB
/
Copy pathControlPageCard.cs
File metadata and controls
83 lines (69 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.ComponentModel;
using System.Diagnostics;
using Avalonia.Media;
using PleasantUI.Core.Localization;
using PleasantUI.Example.Interfaces;
using PleasantUI.Example.Messages;
using PleasantUI.ToolKit.Services.Interfaces;
namespace PleasantUI.Example.Models;
public class ControlPageCard : INotifyPropertyChanged
{
private readonly IEventAggregator _eventAggregator;
private string _title;
private string _description;
public event PropertyChangedEventHandler? PropertyChanged;
public string TitleKey { get; }
public string DescriptionKey { get; }
public Geometry Icon { get; set; }
public Func<IPage> Page { get; set; }
public string Title
{
get => _title;
private set
{
if (_title == value) return;
_title = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Title)));
}
}
public string Description
{
get => _description;
private set
{
if (_description == value) return;
_description = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Description)));
}
}
public ControlPageCard(string titleKey, Geometry icon, string descriptionKey, Func<IPage> page, IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
TitleKey = titleKey;
DescriptionKey = descriptionKey;
Icon = icon;
Page = page;
// Read translated values immediately — satellite DLL must already be loaded by now
_title = Resolve(titleKey);
_description = Resolve(descriptionKey);
Debug.WriteLine($"[ControlPageCard] Created key={titleKey} title=\"{_title}\"");
Localizer.Instance.LocalizationChanged += OnLanguageChanged;
}
private void OnLanguageChanged(string lang)
{
void Update()
{
Title = Resolve(TitleKey);
Description = Resolve(DescriptionKey);
Debug.WriteLine($"[ControlPageCard] Updated key={TitleKey} title=\"{_title}\" lang={lang}");
}
if (Avalonia.Threading.Dispatcher.UIThread.CheckAccess())
Update();
else
Avalonia.Threading.Dispatcher.UIThread.Post(Update);
}
private static string Resolve(string key) =>
Localizer.Instance.TryGetString(key, out string value) ? value : key;
public void OpenPage() =>
_eventAggregator.PublishAsync(new ChangePageMessage(Page.Invoke()));
}