2012-05-22 4 views
0

WPF를 사용하여 .net에서 Windows 응용 프로그램을 개발하려고합니다. 그래서 런타임에 동적 테마를 구현할 수 있습니다. 나는 이것에 관해 많은 것을 조사했다. 그러나 나는이 물건을 이해할 수 없다. app.xaml에 아래 줄을 추가하면 어떻게하면 직접 thing 줄을 추가 할 수 있는지 오류가 표시됩니다. "ExpressionDark"의 이름을 가진 파일은 없지만. 이 같은 App.Xaml의 테마를 병합 할 수 있습니다 :WPF의 동적 테마

답변

0

를 추가 전환하려면 - 시간, 가능한 가장 좋은 방법은 자원 사전, 컨트롤 스타일의 전체 메인 애플 리케이션이나 모든 컨트롤의 리소스에로드합니다.

public static ResourceDictionary GetThemeResourceDictionary(Uri theme) 
    { 
     if (theme != null) 
     { 
      return Application.LoadComponent(theme) as ResourceDictionary; 
     } 
     return null; 
    } 

    public static void ApplyTheme(this ContentControl control /* Change this to Application to use this function at app level */, string theme) 
    { 
     ResourceDictionary dictionary = GetThemeResourceDictionary(theme); 

     if (dictionary != null) 
     { 
      // Be careful here, you'll need to implement some logic to prevent errors. 
      control.Resources.MergedDictionaries.Clear(); 
      control.Resources.MergedDictionaries.Add(dictionary); 
      // For app level 
      // app.Resources.MergedDictionaries.Clear(); 
      // app.Resources.MergedDictionaries.Add(dictionary); 

     } 
    } 
0

사전에

<ResourceDictionary Source="Themes/ExpressionDark.xaml"/> 
***or*** 
<ResourceDictionary Source="ExpressionDark.xaml"/> 

감사 :

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

defaulttheme.xaml 파일은 프로젝트의 루트에 있어야한다 . 당신이 일 테마에 대한 자신의 프로젝트를 빌드 싶은 경우에 당신은이 같은 자원을 병합 할 수 있습니다 :

여기
<ResourceDictionary Source="/MyThemeProject;component/defaulttheme.xaml" />  

defaulthteme.xaml 또한 루트에 MyThemeProject에 있어야하고 추가하는 것을 잊지에서 해당 프로젝트에 대한 참조를 추가하지 마십시오 귀하의 주요 프로젝트.

구조를 만들려면 원하는대로 폴더를 추가 할 수 있습니다.

<ResourceDictionary Source="/MyThemeProject;component/Folder1/Folder2/defaulttheme.xaml" /> 

는 테마 먼저 MergedDictionaries을 취소하고 DynamicThemes에 의해, 당신은 실행에 테마를 넣어 의미한다고 가정하면 새 테마

NewTheme = new Uri(@"/MyThemeProject;component/folder1/Folder2/bluetheme.xaml", UriKind.Relative); 

Application.Current.Resources.MergedDictionaries.Clear(); 
Application.Current.Resources.MergedDictionaries.Add(NewTheme); 

감사

flusser