다음은 테마를 지원하는 애플리케이션에서 사용한 코드 스 니펫입니다. 이 예제에서는 두 가지 테마 (기본 및 클래식 XP)가 있습니다. 테마 리소스는 각각 DefaultTheme.xaml 및 ClassicTheme.xaml에 저장됩니다.
이
내가 테마를 변경할 수 있도록하기 위해 다음과 같은 방법을 가지고있는 App.xaml의 뒤에있는 코드에서 다음 내 App.xaml
<Application ...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ArtworkResources.xaml" />
<ResourceDictionary Source="DefaultTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="SwooshButton" TargetType="ButtonBase">
<!-- style setters -->
</Style>
<!-- more global styles -->
</ResourceDictionary>
</Application.Resources>
</Application>
의 기본 코드입니다. 기본적으로, 당신은 리소스 사전을 지운 다음 새 테마로 사전을 다시로드합니다.
private Themes _currentTheme = Themes.Default;
public Themes CurrentTheme
{
get { return _currentTheme; }
set { _currentTheme = value; }
}
public void ChangeTheme(Themes theme)
{
if (theme != _currentTheme)
{
_currentTheme = theme;
switch (theme)
{
default:
case Themes.Default:
this.Resources.MergedDictionaries.Clear();
AddResourceDictionary("ArtworkResources.xaml");
AddResourceDictionary("DefaultTheme.xaml");
break;
case Themes.Classic:
this.Resources.MergedDictionaries.Clear();
AddResourceDictionary("ArtworkResources.xaml");
AddResourceDictionary("ClassicTheme.xaml");
break;
}
}
}
void AddResourceDictionary(string source)
{
ResourceDictionary resourceDictionary = Application.LoadComponent(new Uri(source, UriKind.Relative)) as ResourceDictionary;
this.Resources.MergedDictionaries.Add(resourceDictionary);
}
이 접근법에서 유의해야 할 점은 테마를 사용하는 모든 스타일에는 동적 리소스가 있어야한다는 것입니다. 예를 들면 다음과 같습니다.
<Window Background="{DynamicResource AppBackgroundColor}" />
위의 대답을 구현 한 결과, 바운드 버튼 이미지를 변경하는 방법을 궁금합니다. 나는 여기에 질문을했습니다 : http://stackoverflow.com/questions/39795317/how-do-i-dynamically-change-which-resource-folder-i-get-an-image-from – AidanO