33using CommunityToolkit . Mvvm . ComponentModel ;
44using CommunityToolkit . Mvvm . Input ;
55using AutoSettingUI . Avalonia . CrossPlatform . Demo . Models ;
6- using AutoSettingUI . Avalonia . CrossPlatform . Demo . Resources ;
7- using AutoSettingUI . Core . Interfaces ;
8- using AutoSettingUI . Core . Services ;
96using Avalonia ;
107using Avalonia . Styling ;
8+ using DynamicLocalization . Core ;
119
1210namespace AutoSettingUI . Avalonia . CrossPlatform . Demo . ViewModels ;
1311
14- /// <summary>
15- /// Main view model for the Avalonia cross-platform demo application.
16- /// Demonstrates dynamic language switching, theme management, and settings panel features.
17- /// </summary>
1812public partial class MainViewModel : ViewModelBase
1913{
20- /// <summary>
21- /// Collection of setting objects to display in the settings panel.
22- /// Each object represents a different settings category.
23- /// </summary>
2414 [ ObservableProperty ]
2515 private ObservableCollection < object > _targets = new ( ) ;
2616
27- /// <summary>
28- /// The title displayed at the top of the settings panel.
29- /// </summary>
3017 [ ObservableProperty ]
3118 private string _title = "Settings" ;
3219
33- /// <summary>
34- /// Controls whether the navigation sidebar is visible.
35- /// </summary>
3620 [ ObservableProperty ]
3721 private bool _showNavigation = true ;
3822
39- /// <summary>
40- /// Width of the navigation sidebar in pixels.
41- /// </summary>
4223 [ ObservableProperty ]
4324 private double _navigationWidth = 200 ;
4425
45- /// <summary>
46- /// Toggles between default and custom styled views.
47- /// </summary>
4826 [ ObservableProperty ]
4927 private bool _isCustomView = false ;
5028
51- /// <summary>
52- /// Settings for extended UI controls demonstration.
53- /// </summary>
5429 [ ObservableProperty ]
5530 private ExtendedControlsSettings _extendedSettings = new ( ) ;
5631
57- /// <summary>
58- /// Settings for theme selection.
59- /// </summary>
6032 [ ObservableProperty ]
6133 private ThemeSettings _themeSettings = new ( ) ;
6234
63- /// <summary>
64- /// Localization service for dynamic language switching.
65- /// </summary>
6635 [ ObservableProperty ]
67- private ILocalizationService ? _localizationService ;
36+ private ICultureService _localizationService ;
6837
69- /// <summary>
70- /// Current language code (e.g., "en", "zh-CN").
71- /// Used to highlight the active language button.
72- /// </summary>
7338 [ ObservableProperty ]
7439 private string _currentLanguage = "en" ;
7540
76- /// <summary>
77- /// Dynamic text for the view toggle button.
78- /// </summary>
7941 public string ViewModeText => IsCustomView ? "Switch to Default View" : "Switch to Custom View" ;
8042
8143 partial void OnIsCustomViewChanged ( bool value )
8244 {
8345 OnPropertyChanged ( nameof ( ViewModeText ) ) ;
8446 }
8547
86- public MainViewModel ( )
48+ public MainViewModel ( ICultureService localizationService )
8749 {
88- LocalizationService = new ResxLocalizationService ( Strings . ResourceManager ) ;
50+ LocalizationService = localizationService ;
8951
9052 Targets . Add ( ThemeSettings ) ;
9153 Targets . Add ( ExtendedSettings ) ;
@@ -96,9 +58,6 @@ public MainViewModel()
9658 ThemeSettings . PropertyChanged += OnThemeSettingsChanged ;
9759 }
9860
99- /// <summary>
100- /// Handles theme changes and applies them to the application.
101- /// </summary>
10261 private void OnThemeSettingsChanged ( object ? sender , System . ComponentModel . PropertyChangedEventArgs e )
10362 {
10463 if ( e . PropertyName == nameof ( ThemeSettings . SelectedTheme ) )
@@ -116,80 +75,51 @@ private void OnThemeSettingsChanged(object? sender, System.ComponentModel.Proper
11675 }
11776 }
11877
119- /// <summary>
120- /// Adds a new ApplicationSettings instance to the targets collection.
121- /// </summary>
12278 [ RelayCommand ]
12379 private void AddApp ( )
12480 {
12581 Targets . Add ( new ApplicationSettings ( ) { AppName = "New App" , Version = "2.0.0" } ) ;
12682 }
12783
128- /// <summary>
129- /// Adds a new UserPreferences instance to the targets collection.
130- /// </summary>
13184 [ RelayCommand ]
13285 private void AddUser ( )
13386 {
13487 Targets . Add ( new UserPreferences ( ) { Language = "Spanish" , FontSize = 16 } ) ;
13588 }
13689
137- /// <summary>
138- /// Adds a new NetworkSettings instance to the targets collection.
139- /// </summary>
14090 [ RelayCommand ]
14191 private void AddNetworkConfig ( )
14292 {
14393 Targets . Add ( new NetworkSettings ( ) { Port = 8080 } ) ;
14494 }
14595
146- /// <summary>
147- /// Toggles between default and custom styled settings panel views.
148- /// </summary>
14996 [ RelayCommand ]
15097 private void ToggleView ( )
15198 {
15299 IsCustomView = ! IsCustomView ;
153100 }
154101
155- /// <summary>
156- /// Toggles the visibility of the navigation sidebar.
157- /// </summary>
158102 [ RelayCommand ]
159103 private void ToggleNavigation ( )
160104 {
161105 ShowNavigation = ! ShowNavigation ;
162106 }
163107
164- /// <summary>
165- /// Switches the application language to English.
166- /// </summary>
167108 [ RelayCommand ]
168109 private void SwitchToEnglish ( )
169110 {
170- LocalizationService ? . SetCulture ( "en" ) ;
111+ LocalizationService . SetCulture ( "en" ) ;
171112 CurrentLanguage = "en" ;
172113 }
173114
174- /// <summary>
175- /// Switches the application language to Chinese (Simplified).
176- /// </summary>
177115 [ RelayCommand ]
178116 private void SwitchToChinese ( )
179117 {
180- LocalizationService ? . SetCulture ( "zh-CN" ) ;
118+ LocalizationService . SetCulture ( "zh-CN" ) ;
181119 CurrentLanguage = "zh-CN" ;
182120 }
183121}
184122
185- /// <summary>
186- /// Value converter that highlights the currently selected language button.
187- /// Returns Bold font weight for the active language, Normal for others.
188- /// </summary>
189- /// <example>
190- /// Usage in XAML:
191- /// TextBlock FontWeight="{Binding CurrentLanguage, Converter={StaticResource LanguageToFontWeightConverter}, ConverterParameter=en}"
192- /// </example>
193123public class LanguageToFontWeightConverter : global ::Avalonia . Data . Converters . IValueConverter
194124{
195125 public object ? Convert ( object ? value , Type targetType , object ? parameter , System . Globalization . CultureInfo culture )
0 commit comments