2012-11-16 52 views
0

我正在WPF應用程序上工作,我試圖在運行時更改顏色。我試着在這個帖子中發佈的解決方案:WPF: Changing Resources (colors) from the App.xaml during runtime;然而,解決方案似乎並不奏效。如何在運行時更改顏色主題

我有3個資源字典定義

Colors1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Color x:Key="BaseColor">#ffaa01</Color> 
    <SolidColorBrush x:Key="BackgroundColorBrush" Color="{DynamicResource BaseColor}" /> 
</ResourceDictionary> 

Colors2.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<Color x:Key="BaseColor">#aaff01</Color> 
<SolidColorBrush x:Key="BackgroundColorBrush" Color="{DynamicResource BaseColor}" /> 
</ResourceDictionary> 

Colors3.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<Color x:Key="BaseColor">#aa01ff</Color> 
<SolidColorBrush x:Key="BackgroundColorBrush" Color="{DynamicResource BaseColor}" /> 
</ResourceDictionary> 

所有3個資源字典是LOCA在我的應用程序的根目錄下名爲「資源」的子文件夾中。

我已將Colors1資源字典添加到應用程序資源(在Application.xaml文件中),以便默認加載它。

這裏是我的Application.xaml:

<Application x:Class="Application" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
StartupUri="Window1.xaml"> 
    <Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary x:Name="Colors1" Source="Resources/Colors1.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
    </Application.Resources> 
</Application> 

我在所謂的「窗口1」,其中包含一個網格和組合框,可以讓你的資源字典之間切換應用程序的窗口。

下面是窗口1的XAML代碼:

<Window x:Class="Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="Picture Orders" Height="600" Width="600" 
xmlns:myProj="clr-namespace:TryingWPF"> 
<Window.Resources> 
    <x:Array x:Key="ResourceNames" Type="sys:String" 
      xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
     <sys:String>Colors1</sys:String> 
     <sys:String>Colors2</sys:String> 
     <sys:String>Colors3</sys:String> 
    </x:Array> 
</Window.Resources> 
<Grid x:Name="VisualRoot" Background="{DynamicResource BackgroundColorBrush}"> 
    <ComboBox x:Name="ResourceOptions" 
       ItemsSource="{StaticResource ResourceNames}" 
       SelectedIndex="0" 
       VerticalAlignment="Top" 
       HorizontalAlignment="Center" 
       SelectionChanged="ResourceOptions_SelectionChanged"/> 
</Grid> 
</Window> 

這裏是VB.NET代碼,後者用於處理要改變背景顏色在窗口網格組合框中選擇改變事件:

Public Class Window1 

    Private Sub ResourceOptions_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) 
    Dim resourceSource As String = String.Format("pack://application:,,,/Resources/{0}.xaml", ResourceOptions.SelectedValue) 
    Dim newResourceDictionary As New ResourceDictionary() 
    newResourceDictionary.Source = New Uri(resourceSource) 
    Application.Current.Resources.MergedDictionaries.RemoveAt(0) 
    Application.Current.Resources.MergedDictionaries.Insert(0, newResourceDictionary) 

    End Sub 
End Class 

我不知道我在做什麼錯。

如何獲取的顏色改變?

*編輯:* 好了,發佈了這個問題之後,我發現,如果我搬到刷出來的顏色資源文件,併到窗口資源,顏色會在切換資源改變。

但是,如果我將畫筆移動到另一個名爲ColorBrushes的ResourceDictionary,並且只需切換Color ResourceDictionaries,它將不會更改顏色。

在我的主應用程序中,我的所有畫筆都是在ResourceDictionaries中定義的,而不是在Windows或用戶控件本身中定義的....所以這仍然是我的問題。

+0

我做類似的在我的應用程序來處理幾種語言的東西,和它的作品...我看到的唯一區別是我在刪除舊的資源之前添加新的資源詞典,因爲我不希望資源在短時間內未定義。 (萬一它引發異常或類似情況)。值得嘗試。另一個想法是:在更改dictionnary之前/之後使用debug,並觀察資源的值(Me.TryFindResource(key))。 – GameAlchemist

回答

1

我通過改變顏色字典,然後改變筆刷字典畫筆字典解決了這個問題。

像這樣:

Public Class Window1 

Private Sub ResourceOptions_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) 
    Dim resourceSource As String = String.Format("pack://application:,,,/Resources/{0}.xaml", ResourceOptions.SelectedValue) 
    Dim newResourceDictionary As New ResourceDictionary() 
    newResourceDictionary.Source = New Uri(resourceSource) 
    Application.Current.Resources.MergedDictionaries.RemoveAt(0) 
    Application.Current.Resources.MergedDictionaries.Insert(0, newResourceDictionary) 

    Dim brushesResourceSource As String = "pack://application:,,,/Resources/ColorBrushes.xaml" 
    Dim newBrushesResourceDictionary As New ResourceDictionary() 
    newBrushesResourceDictionary.Source = New Uri(brushesResourceSource) 
    Application.Current.Resources.MergedDictionaries.RemoveAt(1) 
    Application.Current.Resources.MergedDictionaries.Insert(1, brushesResourceSource) 
End Sub 
End Class 

-Frinny