【问题标题】:How to Change Theme XAML with ComboBox?如何使用 ComboBox 更改主题 XAML?
【发布时间】:2017-04-09 12:55:29
【问题描述】:

如何使用 ComboBox 将不同的颜色主题应用于我的程序?

ThemeBlue.xamlThemeRed.xamlThemePurple.xaml

我需要它在选择更改时将一个主题 xaml 文件换成另一个。

这是示例项目文件:
https://drive.google.com/open?id=0BycnCTAQ1U7gSU5kUUdaNzRIZDg


主题

我有一个主题文件ThemeBlue.xaml,为控件和窗口设置了颜色。

<SolidColorBrush x:Key="TextBoxCustom.Static.Background" Color="Blue"/>
<SolidColorBrush x:Key="TextBoxCustom.Static.Border" Color="Blue"/>

<Style x:Key="TextBoxCustom" TargetType="{x:Type TextBox}">
    <Setter Property="Background" Value="{StaticResource TextBoxCustom.Static.Background}"/>
    <Setter Property="BorderBrush" Value="{StaticResource TextBoxCustom.Static.Border}"/>
...

组合框主题选择

XAML

<ComboBox x:Name="cboTheme" Style="{StaticResource ComboBoxCustom}" HorizontalAlignment="Left" Margin="199,120,0,0" VerticalAlignment="Top" Width="75" SelectionChanged="themeSelect_SelectionChanged">
    <System:String>Blue</System:String>
    <System:String>Red</System:String>
    <System:String>Purple</System:String>
</ComboBox>

C#

这是我需要帮助来应用主题文件的地方。

private void themeSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // Apply theme file to App.xaml from option selected
    // Blue
    // Red
    // Purple
}

App.xaml

启动时通过App.xaml加载主题文件

<Application x:Class="MyProgram.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">

    <Application.Resources>
        <ResourceDictionary Source="ThemeBlue.xaml"/>
    </Application.Resources>

</Application>

MainWindow.xaml

应用了主题样式的文本框:

<TextBox x:Name="textBox1" Style="{StaticResource TextBoxCustom}" HorizontalAlignment="Left" Height="22" Margin="93,43,0,0" Width="464" />

保存主题

我通过设置保存和加载主题。

private void themeSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // Save Theme for next launch
    Settings.Default["Theme"] = cboTheme.SelectedItem.ToString();
    Settings.Default.Save();
    Settings.Default.Reload();
}

【问题讨论】:

    标签: c# wpf visual-studio xaml


    【解决方案1】:

    试试这个:

    private void themeSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string theme = cboTheme.SelectedItem.ToString();
    
        App.Current.Resources.MergedDictionaries.Clear();
        App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Theme" + theme + ".xaml", UriKind.RelativeOrAbsolute) });
        // Save Theme for next launch
        Settings.Default["Theme"] = theme;
        Settings.Default.Save();
    }
    

    App.xaml:

    <Application x:Class="MyProgram.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml">
    
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="ThemeBlue.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    
    </Application>
    

    【讨论】:

    • 当我制作 ThemeRed.xaml 并包含在项目中时,我在编译时遇到这些错误。 i.imgur.com/A3NvqrA.png
    • 您对主窗口做了哪些更改!?显然我发布的示例代码与这些错误无关。
    • 我没有对 MainWindow 进行任何更改。这是代码kopy.io/fuXE7
    • 不,这是标记...如果您需要任何进一步的帮助,请尝试重建您的项目并上传您的问题的完整可重现示例。
    • 从资源字典(ThemeBlue.xaml 和 ThemeRed.xaml)中删除 x:Class="WpfApplication23.MainWindow" 属性,它就会起作用。
    【解决方案2】:

    您需要在后面的代码中将 App.xaml 的资源替换为另一个资源。您可以通过加载组件来获取资源。这是一个可能的解决方案:

    private void themeSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        AssemblyName assemblyName = Assembly.GetAssembly(this.GetType()).GetName();
    
        ResourceDictionary dictionary = (ResourceDictionary)Application.LoadComponent(
            new Uri(assemblyName.Name + @";component/" + cboTheme.SelectedItem + ".xaml", UriKind.Relative))));
    
        App.Current.Resources.MergedDictionaries.Clear();
        if (dictionary != null)
        {
            App.Current.Resources.MergedDictionaries.Add(dictionary);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      相关资源
      最近更新 更多