I would like to give the user an option to change the theme from dark to light, by clicking the button.
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp"
x:Class="CoreBackup.App">
<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>
<Application.Styles>
<StyleInclude Source="avares://Avalonia.Themes.Default/DefaultTheme.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Accents/BaseDark.xaml"/>
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Default.xaml"/>
</Application.Styles>
How I can get access from other ViewModel to the Application.Styles?
I don't know if the problem is solved. So may be this helps to switch the theme from BaseDark to BaseLight.
private void ChangeLayoutExecute(object o)
{
// create new style
var newStyle = new StyleInclude(new Uri("avares://AvaloniaApplicationTest/App.xaml"));
newStyle.Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseLight.xaml");
// load style to get access to the ressources
var baseDarkStyle = newStyle.Loaded as Style;
// get the original source (BaseDark)
var ressourceFromAppXaml = ((Style)((StyleInclude)Application.Current.Styles[1]).Loaded).Resources;
foreach (var item in baseDarkStyle.Resources)
{
// for secure lookup if the key exists for the resource otherwise create it
if (ressourceFromAppXaml.ContainsKey(item.Key))
ressourceFromAppXaml[item.Key] = item.Value;
else
ressourceFromAppXaml.Add(item.Key, item.Value);
}
// set source name for the new theme
((StyleInclude)Application.Current.Styles[1]).Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseLight.xaml");
}