gpt4 book ai didi

wpf - WPF将主题应用于特定项目而不是整个应用程序

转载 作者:行者123 更新时间:2023-12-03 10:38:00 25 4
gpt4 key购买 nike

我在project1中创建了一个主题,并在app.xaml中引用了theme.xaml。结果是所有项目在解决方案中都具有相同的主题。

将theme.xaml应用于指定项目(即仅应用于project1但不应用于project2)的最简单方法是什么?

我知道我可以使用以下方式在project1中的每个WFP表单中引用theme.xaml

    <Window.Resources>
<ResourceDictionary Source="/Project1;component/Themes/Customized.xaml" />
</Window.Resources>

但是,如果我想更改项目主题,那将很难维护。我正在寻找的是类似于project.xaml的东西,其行为类似于app.xaml,只是作用域是当前项目。这样,我可以在一个地方为指定项目(而不是其他项目)引用theme.xaml。

那可能吗?

提前致谢。

最佳答案

  • 创建项目主题资源字典,并将对FooTheme.xaml的引用放入其中。
  • 在项目的所有窗口中,放置对ProjectTheme.xaml的引用。

  • 这样,为了更改项目的主题,您只需要修改一行。

    代码:

    FooTheme.xaml (示例主题)
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Button">
    <Setter Property="Background" Value="Blue"/>
    </Style>
    </ResourceDictionary>

    ProjectTheme.xaml (项目主题)
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
    <!-- In order to modify the project's theme, change this line -->
    <ResourceDictionary Source="FooTheme.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

    MainWindow.xaml (示例项目窗口)
    <Window x:Class="So17372811ProjectTheme.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
    <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="ProjectTheme.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    </Window.Resources>
    <Grid>
    <Button Content="Click me!"/>
    </Grid>
    </Window>

    关于wpf - WPF将主题应用于特定项目而不是整个应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17372811/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com