繁体   English   中英

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");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM