WPF는 "리소스 중심"입니다. 리소스의 모든 UI 스타일, 브러쉬 및 템플릿을 정의하고 런타임에 언급 한 모든 속성을 포함하는 응용 프로그램 차원의 테마 변경을 매우 쉽게 수행 할 수 있습니다. 그것의 SettingsViewModel를 통해 내 설정 창에서 메시지를 수신 한 후 여기에 내 MainViewModel에서 그것을 할 방법은 다음과 같습니다 분명히
private void ApplyTheme()
{
Application.Current.Resources.MergedDictionaries.Clear();
var rd = new ResourceDictionary { { "Locator", locator } };
Application.Current.Resources.MergedDictionaries.Add(rd);
switch (theme)
{
case "Blue":
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute) });
break;
case "Summer":
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute) });
break;
}
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Brushes.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/ControlTemplates.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/DataTemplates.xaml", UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Styles.xaml", UriKind.RelativeOrAbsolute) });
}
그래서 나는 그들의 사전로드하지만, 방법의 하단에 당신이거야 제어합니다 Telerik를 사용하고 있습니다 브러시, 스타일 등의 리소스를로드합니다.
결론적으로 WPF에서는 응용 프로그램 차원의 테마 변경이 쉽지 않았습니다.
관련 코드와 XAML을 게시하십시오. BTW WPF는 [테마에 대한 내장 지원] (http://stackoverflow.com/a/11139598/643085)을 가지고 있는데, 이것은 공룡 winforms에서 완전히 빠져 있습니다. 그래서 당신은 winforms에서 모든 종류의 끔찍한 해킹을 강요 당했고, WPF에서 모든 것이 아름답고 행복합니다. –
테마 트릭은 'HandledWindow'의 현재 인스턴스 리소스를 대체하여 수행됩니다. 그 역시 생각했습니다.하지만 데이터 바인딩과 같은 순간에 다른 윈도우의 테마를 변경하는 무언가가 필요합니다. C# 코드만으로 모든 XAML 페이지, 속성에 대한 새 값 설정 및 시각적 UI가 동시에 업데이트되어야합니다. 몇 분 안에 코드를 게시 할 것입니다. 이러한 정적 공용 속성도 UI로 편집 할 수 있어야합니다. 즉, UI에서 설정하기 위해 다른 데이터 바인딩이 있어야 함을 의미합니다. – Lispwave
아니요. 애플리케이션 전반의 테마를 원하면 'System.Windows.Application.Current.Resources'에 리소스 사전을로드하십시오. –