Dynamically change implicit style

我是研究僧i 提交于 2019-12-23 02:55:09

问题


Right now I have some TabItems in my App which are implicitly styled. I want to add a "Night mode" to my app and change my style. How should I go about this?


回答1:


You can do this with merged dictionaries. Put all your "normal" styles in a dictionary and add it to the app resources by default:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles/Normal.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Then you can remove the current dictionary and load another one dynamically:

private void ChangeStyles()
{
    App.Current.Resources.MergedDictionaries.Clear();

    StreamResourceInfo resInfo = App.GetResourceStream(new Uri("Styles/NewStyles.xaml", UriKind.Relative));
    XDocument xaml = XDocument.Load(resInfo.Stream);
    ResourceDictionary resource = XamlReader.Load(xaml.ToString()) as ResourceDictionary;

    App.Current.Resources.MergedDictionaries.Add(resource);
}



回答2:


Alfonso was right in idea... but you have to do it like this in WPF

App.Current.Resources.MergedDictionaries.Clear(); 
Uri uri = new Uri("/Resources/GlassButton5Night.xaml", UriKind.Relative);
var resDict = Application.LoadComponent(uri) as ResourceDictionary;
App.Current.Resources.MergedDictionaries.Add(resDict);

And you have make sure you reset your MergedDictionaries at the right level



来源:https://stackoverflow.com/questions/7276055/dynamically-change-implicit-style

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!