WPF - 通过单击适当的按钮将主题从深色更改为浅色 - Avalonia

WPF - Change theme from dark to light by clicking proper button - Avalonia

我想通过单击按钮为用户提供将主题从深色更改为浅色的选项。

<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>

如何从其他 ViewModel 访问 Application.Styles?

不知道问题解决了没有。所以这可能有助于将主题从 BaseDark 切换到 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");
}